本文整理汇总了Java中org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory类的典型用法代码示例。如果您正苦于以下问题:Java WebSocketServerHandshakerFactory类的具体用法?Java WebSocketServerHandshakerFactory怎么用?Java WebSocketServerHandshakerFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WebSocketServerHandshakerFactory类属于org.jboss.netty.handler.codec.http.websocketx包,在下文中一共展示了WebSocketServerHandshakerFactory类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleRequest
import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //导入依赖的package包/类
/**
* Processes an HTTP request
* @param ctx The channel handler context
* @param req The HTTP request
* @param me The message event that will be sent upstream if not handled here
*/
public void handleRequest(ChannelHandlerContext ctx, HttpRequest req, MessageEvent me) {
log.warn("HTTP Request: {}", req);
if (req.getMethod() != GET) {
sendHttpResponse(ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN));
return;
}
String uri = req.getUri();
if(!"/ws".equals(uri)) {
ctx.sendUpstream(me);
return;
}
final Channel channel = me.getChannel();
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, false);
WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
wsFactory.sendUnsupportedWebSocketVersionResponse(channel);
} else {
wsHandShaker.set(channel, handshaker);
ChannelFuture cf = handshaker.handshake(channel, req);
cf.addListener(WebSocketServerHandshaker.HANDSHAKE_LISTENER);
cf.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture f) throws Exception {
if(f.isSuccess()) {
final Channel wsChannel = f.getChannel();
// RPCSessionManager.getInstance().getSession(wsChannel).addSessionAttribute(RPCSessionAttribute.Protocol, "WebSocket");
// SharedChannelGroup.getInstance().add(
// f.getChannel(),
// ChannelType.WEBSOCKET_REMOTE,
// "WebSocketClient-" + f.getChannel().getId(),
// ((InetSocketAddress)wsChannel.getRemoteAddress()).getAddress().getCanonicalHostName(),
// "WebSock[" + wsChannel.getId() + "]"
// );
//wsChannel.write(new JSONObject(Collections.singletonMap("sessionid", wsChannel.getId())));
wsChannel.write(marshaller.getNodeFactory().objectNode().put("sessionid", "" + wsChannel.getId()));
//wsChannel.getPipeline().remove(DefaultChannelHandler.NAME);
}
}
});
}
}
示例2: handleHttpRequest
import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //导入依赖的package包/类
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
if (req.getMethod() != GET) {
sendHttpResponse(ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN));
return;
}
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, false);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel());
} else {
handshaker.handshake(ctx.getChannel(), req).addListener(WebSocketServerHandshaker.HANDSHAKE_LISTENER);
}
}
示例3: handleHttpRequest
import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //导入依赖的package包/类
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) {
this.dump(req);
if (req.getMethod() != HttpMethod.GET) {
this.sendHttpResponse(ctx, req,
new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN));
return;
}
this.renderServerChannelContext(req);
if (this.channelHandler != null) {
try {
this.channelHandler.onConnect(this.createContext(req.getHeaders()));
} catch (Exception e) {
this.logger.error(e);
this.sendHttpResponse(ctx, req, new DefaultHttpResponse(HttpVersion.HTTP_1_1,
new HttpResponseStatus(401, e.getMessage())));
return;
}
}
String subprotocols = null;
boolean allowExtensions = false;
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
req.getUri(), subprotocols, allowExtensions);
this.handshaker = wsFactory.newHandshaker(req);
if (this.handshaker == null) {
wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel());
return;
}
// FIXME: maybe not finish for later work
this.handshaker.handshake(ctx.getChannel(), req)
.addListener(WebSocketServerHandshaker.HANDSHAKE_LISTENER);
if (this.cumulative) {
// use custom decoder for cumulative
ctx.getPipeline().replace(WebSocket13FrameDecoder.class, "wsdecoder-custom",
new CustomWebSocket13FrameDecoder(true, allowExtensions,
this.handshaker.getMaxFramePayloadLength()));
}
}
示例4: handleHttpRequest
import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //导入依赖的package包/类
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) throws Exception
{
// Allow only GET methods.
if (req.getMethod() != GET)
{
sendHttpResponse(ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN));
return;
}
// Send the demo page and favicon.ico
if (req.getUri().equals("/"))
{
return;
}
else if (req.getUri().equals("/favicon.ico"))
{
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
sendHttpResponse(ctx, req, res);
return;
}
this.ctx = ctx;
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, false);
this.handshaker = wsFactory.newHandshaker(req);
if (this.handshaker == null)
{
wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel());
}
else
{
ChannelFuture handshake = this.handshaker.handshake(ctx.getChannel(), req);
handshake.addListener(WebSocketServerHandshaker.HANDSHAKE_LISTENER);
handshake.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception
{
WebServerSocketHandler.this.t.notifyOpen();
}
});
}
}
示例5: websocketAccepted
import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //导入依赖的package包/类
@Override
public void websocketAccepted(final WebsocketClient client) {
if (log.isDebugEnabled()) {
log.debug("Socket connected, completing handshake for "
+ websocketChannel.getRemoteAddress());
}
NettyWebsocketClient websocketClient = (NettyWebsocketClient) client;
websocketClient.setWebsocketChannel(websocketChannel);
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
getWebSocketLocation(request.getNettyRequest()), null,
true);
WebSocketServerHandshaker handshaker = wsFactory
.newHandshaker(request.getNettyRequest());
if (handshaker == null) {
wsFactory
.sendUnsupportedWebSocketVersionResponse(websocketChannel);
} else {
handshaker.handshake(websocketChannel,
request.getNettyRequest()).addListener(
new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future)
throws Exception {
if (future.isSuccess()) {
if (log.isDebugEnabled())
log.debug("Handshake complete for "
+ websocketChannel
.getRemoteAddress());
client.open();
} else {
if (log.isDebugEnabled())
log.debug("Handshake failed for "
+ websocketChannel
.getRemoteAddress());
client.close();
}
}
});
}
}