A little bit of documentation
This commit is contained in:
parent
f67de23214
commit
4ab3e248e7
@ -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)
|
||||||
*
|
*
|
||||||
|
@ -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,34 +86,36 @@ 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))) {
|
||||||
HttpResponse res = new DefaultHttpResponse(
|
|
||||||
HttpVersion.HTTP_1_1,
|
|
||||||
new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
|
|
||||||
res.addHeader(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET);
|
|
||||||
res.addHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE);
|
|
||||||
res.addHeader(HttpHeaders.Names.WEBSOCKET_ORIGIN, req.getHeader(HttpHeaders.Names.ORIGIN));
|
|
||||||
res.addHeader(HttpHeaders.Names.WEBSOCKET_LOCATION, getWebSocketLocation(req));
|
|
||||||
String protocol = req.getHeader(HttpHeaders.Names.WEBSOCKET_PROTOCOL);
|
|
||||||
if (protocol != null) {
|
|
||||||
res.addHeader(HttpHeaders.Names.WEBSOCKET_PROTOCOL, protocol);
|
|
||||||
}
|
|
||||||
|
|
||||||
ChannelPipeline p = ctx.getChannel().getPipeline();
|
// Create the WebSocket handshake response.
|
||||||
p.remove("aggregator");
|
HttpResponse res = new DefaultHttpResponse(
|
||||||
p.replace("decoder", "wsdecoder", new WebSocketFrameDecoder());
|
HttpVersion.HTTP_1_1,
|
||||||
|
new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
|
||||||
ctx.getChannel().write(res);
|
res.addHeader(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET);
|
||||||
|
res.addHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE);
|
||||||
p.replace("encoder", "wsencoder", new WebSocketFrameEncoder());
|
res.addHeader(HttpHeaders.Names.WEBSOCKET_ORIGIN, req.getHeader(HttpHeaders.Names.ORIGIN));
|
||||||
|
res.addHeader(HttpHeaders.Names.WEBSOCKET_LOCATION, getWebSocketLocation(req));
|
||||||
return;
|
String protocol = req.getHeader(HttpHeaders.Names.WEBSOCKET_PROTOCOL);
|
||||||
|
if (protocol != null) {
|
||||||
|
res.addHeader(HttpHeaders.Names.WEBSOCKET_PROTOCOL, protocol);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Upgrade the connection and send the handshake response.
|
||||||
|
ChannelPipeline p = ctx.getChannel().getPipeline();
|
||||||
|
p.remove("aggregator");
|
||||||
|
p.replace("decoder", "wsdecoder", new WebSocketFrameDecoder());
|
||||||
|
|
||||||
|
ctx.getChannel().write(res);
|
||||||
|
|
||||||
|
p.replace("encoder", "wsencoder", new WebSocketFrameEncoder());
|
||||||
|
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);
|
||||||
|
@ -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)
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user