A little bit of documentation

This commit is contained in:
Trustin Lee 2010-01-09 12:12:50 +00:00
parent f67de23214
commit 4ab3e248e7
3 changed files with 38 additions and 25 deletions

View File

@ -22,6 +22,13 @@ import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
/** /**
* An HTTP server which serves Web Socket requests at:
*
* http://localhost:8080/websocket
*
* Open your browser at http://localhost:8080/, then the demo page will be
* loaded and a Web Socket connection will be made automatically.
*
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (trustin@gmail.com) * @author Trustin Lee (trustin@gmail.com)
* *

View File

@ -68,7 +68,7 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
return; return;
} }
// Send the example web page. // Send the demo page.
if (req.getUri().equals("/")) { if (req.getUri().equals("/")) {
HttpResponse res = new DefaultHttpResponse( HttpResponse res = new DefaultHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK); HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
@ -86,10 +86,12 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
return; return;
} }
// Serve the WebSocket connection. // Serve the WebSocket handshake request.
if (req.getUri().equals(WEBSOCKET_PATH)) { if (req.getUri().equals(WEBSOCKET_PATH) &&
if (HttpHeaders.Values.UPGRADE.equalsIgnoreCase(req.getHeader(HttpHeaders.Names.CONNECTION)) && HttpHeaders.Values.UPGRADE.equalsIgnoreCase(req.getHeader(HttpHeaders.Names.CONNECTION)) &&
HttpHeaders.Values.WEBSOCKET.equalsIgnoreCase(req.getHeader(HttpHeaders.Names.UPGRADE))) { HttpHeaders.Values.WEBSOCKET.equalsIgnoreCase(req.getHeader(HttpHeaders.Names.UPGRADE))) {
// Create the WebSocket handshake response.
HttpResponse res = new DefaultHttpResponse( HttpResponse res = new DefaultHttpResponse(
HttpVersion.HTTP_1_1, HttpVersion.HTTP_1_1,
new HttpResponseStatus(101, "Web Socket Protocol Handshake")); new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
@ -102,6 +104,7 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
res.addHeader(HttpHeaders.Names.WEBSOCKET_PROTOCOL, protocol); res.addHeader(HttpHeaders.Names.WEBSOCKET_PROTOCOL, protocol);
} }
// Upgrade the connection and send the handshake response.
ChannelPipeline p = ctx.getChannel().getPipeline(); ChannelPipeline p = ctx.getChannel().getPipeline();
p.remove("aggregator"); p.remove("aggregator");
p.replace("decoder", "wsdecoder", new WebSocketFrameDecoder()); p.replace("decoder", "wsdecoder", new WebSocketFrameDecoder());
@ -109,11 +112,10 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
ctx.getChannel().write(res); ctx.getChannel().write(res);
p.replace("encoder", "wsencoder", new WebSocketFrameEncoder()); p.replace("encoder", "wsencoder", new WebSocketFrameEncoder());
return; return;
} }
}
// Send an error page otherwise.
sendHttpResponse( sendHttpResponse(
ctx, req, new DefaultHttpResponse( ctx, req, new DefaultHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN)); HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN));
@ -126,6 +128,7 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
} }
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) {
// Generate an error page if response status code is not OK (200).
if (res.getStatus().getCode() != 200) { if (res.getStatus().getCode() != 200) {
res.setContent( res.setContent(
ChannelBuffers.copiedBuffer( ChannelBuffers.copiedBuffer(
@ -135,6 +138,7 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
Integer.toString(res.getContent().readableBytes())); Integer.toString(res.getContent().readableBytes()));
} }
// Send the response and close the connection if necessary.
ChannelFuture f = ctx.getChannel().write(res); ChannelFuture f = ctx.getChannel().write(res);
if (!req.isKeepAlive() || res.getStatus().getCode() != 200) { if (!req.isKeepAlive() || res.getStatus().getCode() != 200) {
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);

View File

@ -21,6 +21,8 @@ import org.jboss.netty.util.CharsetUtil;
/** /**
* Generates the demo HTML page which is served at http://localhost:8080/
*
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (trustin@gmail.com) * @author Trustin Lee (trustin@gmail.com)
* *