Fixed web service client. Added more package documentation.

This commit is contained in:
Veebs 2011-10-17 16:49:19 +11:00
parent c60b1e28cb
commit 12d93cca29
3 changed files with 30 additions and 73 deletions

View File

@ -16,31 +16,19 @@
package org.jboss.netty.example.http.websocketx.client; package org.jboss.netty.example.http.websocketx.client;
import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.logging.ConsoleHandler; import java.util.logging.ConsoleHandler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.ChannelGroupFuture;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.example.http.websocketx.server.WebSocketServerPipelineFactory;
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame; import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame; import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame; import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame; import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame; import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketSpecificationVersion; import org.jboss.netty.handler.codec.http.websocketx.WebSocketSpecificationVersion;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
/** /**
* A HTTP client demo app * A HTTP client demo app
@ -51,41 +39,14 @@ import org.jboss.netty.logging.InternalLoggerFactory;
*/ */
public class App { public class App {
private static final ChannelGroup allChannels = new DefaultChannelGroup("App");
private static ChannelFactory channelFactory = null;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ConsoleHandler ch = new ConsoleHandler(); ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(Level.FINE); ch.setLevel(Level.FINE);
Logger.getLogger("").addHandler(ch); Logger.getLogger("").addHandler(ch);
Logger.getLogger("").setLevel(Level.FINE); Logger.getLogger("").setLevel(Level.FINE);
startServer();
runClient(); runClient();
System.exit(0);
stopServer();
}
/**
* Starts our web socket server
*/
public static void startServer() {
// Configure the server.
channelFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
ServerBootstrap bootstrap = new ServerBootstrap(channelFactory);
// Set up the event pipeline factory.
bootstrap.setPipelineFactory(new WebSocketServerPipelineFactory());
// Bind and start to accept incoming connections.
Channel channel = bootstrap.bind(new InetSocketAddress(8080));
allChannels.add(channel);
System.out
.println("Web Socket Server started on 8080. Open your browser and navigate to http://localhost:8080/");
} }
/** /**
@ -104,44 +65,35 @@ public class App {
WebSocketSpecificationVersion.V10, callbackHandler); WebSocketSpecificationVersion.V10, callbackHandler);
// Connect // Connect
System.out.println("WebSocket Client connecting");
client.connect().awaitUninterruptibly(); client.connect().awaitUninterruptibly();
Thread.sleep(200); Thread.sleep(200);
// Send 10 messages and wait for responses // Send 10 messages and wait for responses
System.out.println("WebSocket Client sending message");
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
client.send(new TextWebSocketFrame("Message #" + i)); client.send(new TextWebSocketFrame("Message #" + i));
} }
Thread.sleep(1000); Thread.sleep(1000);
// Close - this throws ClosedChannelException. Not sure why. Just as // Ping
// easy to just disconnect. System.out.println("WebSocket Client sending ping");
client.send(new PingWebSocketFrame(ChannelBuffers.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 }))); client.send(new PingWebSocketFrame(ChannelBuffers.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
Thread.sleep(1000);
// Close
System.out.println("WebSocket Client sending close");
client.send(new CloseWebSocketFrame()); client.send(new CloseWebSocketFrame());
Thread.sleep(200); Thread.sleep(1000);
// Disconnect // Disconnect
client.disconnect(); client.disconnect();
} }
/**
* Stops the server
*/
public static void stopServer() {
ChannelGroupFuture future = allChannels.close();
future.awaitUninterruptibly();
channelFactory.releaseExternalResources();
channelFactory = null;
}
/** /**
* Our web socket callback handler for this app * Our web socket callback handler for this app
*/ */
public static class MyCallbackHandler implements WebSocketCallback { public static class MyCallbackHandler implements WebSocketCallback {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(MyCallbackHandler.class);
public boolean connected = false; public boolean connected = false;
public ArrayList<String> messagesReceived = new ArrayList<String>(); public ArrayList<String> messagesReceived = new ArrayList<String>();
@ -151,13 +103,13 @@ public class App {
@Override @Override
public void onConnect(WebSocketClient client) { public void onConnect(WebSocketClient client) {
logger.debug("WebSocket Client connected!"); System.out.println("WebSocket Client connected!");
connected = true; connected = true;
} }
@Override @Override
public void onDisconnect(WebSocketClient client) { public void onDisconnect(WebSocketClient client) {
logger.debug("WebSocket Client disconnected!"); System.out.println("WebSocket Client disconnected!");
connected = false; connected = false;
} }
@ -165,18 +117,18 @@ public class App {
public void onMessage(WebSocketClient client, WebSocketFrame frame) { public void onMessage(WebSocketClient client, WebSocketFrame frame) {
if (frame instanceof TextWebSocketFrame) { if (frame instanceof TextWebSocketFrame) {
TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
logger.debug("WebSocket Client Received Message:" + textFrame.getText()); System.out.println("WebSocket Client received message:" + textFrame.getText());
messagesReceived.add(textFrame.getText()); messagesReceived.add(textFrame.getText());
} else if (frame instanceof PongWebSocketFrame) { } else if (frame instanceof PongWebSocketFrame) {
logger.debug("WebSocket Client ping/pong"); System.out.println("WebSocket Client received pong");
} else if (frame instanceof CloseWebSocketFrame) { } else if (frame instanceof CloseWebSocketFrame) {
logger.debug("WebSocket Client closing"); System.out.println("WebSocket Client received closing");
} }
} }
@Override @Override
public void onError(Throwable t) { public void onError(Throwable t) {
logger.error("WebSocket Client error", t); System.out.println("WebSocket Client error " + t.toString());
} }
} }

View File

@ -206,10 +206,12 @@ public class WebSocket08FrameDecoder extends ReplayingDecoder<WebSocket08FrameDe
framePayloadLength = framePayloadLen1; framePayloadLength = framePayloadLen1;
} }
logger.debug("Frame length =" + framePayloadLength); //logger.debug("Frame length=" + framePayloadLength);
checkpoint(State.MASKING_KEY); checkpoint(State.MASKING_KEY);
case MASKING_KEY: case MASKING_KEY:
if (this.maskedPayload){
maskingKey = buffer.readBytes(4); maskingKey = buffer.readBytes(4);
}
checkpoint(State.PAYLOAD); checkpoint(State.PAYLOAD);
case PAYLOAD: case PAYLOAD:
// Some times, the payload may not be delivered in 1 nice packet // Some times, the payload may not be delivered in 1 nice packet
@ -218,6 +220,7 @@ public class WebSocket08FrameDecoder extends ReplayingDecoder<WebSocket08FrameDe
ChannelBuffer payloadBuffer = null; ChannelBuffer payloadBuffer = null;
int willHaveReadByteCount = framePayloadBytesRead + rbytes; int willHaveReadByteCount = framePayloadBytesRead + rbytes;
//logger.debug("Frame rbytes=" + rbytes + " willHaveReadByteCount=" + willHaveReadByteCount + " framePayloadLength=" + framePayloadLength);
if (willHaveReadByteCount == framePayloadLength) { if (willHaveReadByteCount == framePayloadLength) {
// We have all our content so proceed to process // We have all our content so proceed to process
payloadBuffer = buffer.readBytes(rbytes); payloadBuffer = buffer.readBytes(rbytes);

View File

@ -93,6 +93,9 @@ public class WebSocket08FrameEncoder extends OneToOneEncoder {
if (msg instanceof WebSocketFrame) { if (msg instanceof WebSocketFrame) {
WebSocketFrame frame = (WebSocketFrame) msg; WebSocketFrame frame = (WebSocketFrame) msg;
ChannelBuffer data = frame.getBinaryData(); ChannelBuffer data = frame.getBinaryData();
if (data == null) {
data = ChannelBuffers.EMPTY_BUFFER;
}
byte opcode; byte opcode;
if (frame instanceof TextWebSocketFrame) { if (frame instanceof TextWebSocketFrame) {
@ -165,7 +168,6 @@ public class WebSocket08FrameEncoder extends OneToOneEncoder {
body = data; body = data;
} }
return ChannelBuffers.wrappedBuffer(header, body); return ChannelBuffers.wrappedBuffer(header, body);
} }
// If not websocket, then just return the message // If not websocket, then just return the message