当前位置: 首页>>代码示例>>Java>>正文


Java WebSocketServerHandshakerFactory类代码示例

本文整理汇总了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);
					}
				}
			});
        }
	}
 
开发者ID:nickman,项目名称:HeliosStreams,代码行数:50,代码来源:WebSocketServiceHandler.java

示例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);
    }
}
 
开发者ID:hou80houzhu,项目名称:rocserver,代码行数:14,代码来源:WebSocketServerHandler.java

示例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()));
    }
}
 
开发者ID:kuiwang,项目名称:my-dev,代码行数:44,代码来源:WebSocketServerUpstreamHandler.java

示例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();
			}
		});
	}
}
 
开发者ID:EricssonResearch,项目名称:trap,代码行数:45,代码来源:WebServerSocketHandler.java

示例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();
						}
					}
				});
	}
}
 
开发者ID:ludup,项目名称:hypersocket-framework,代码行数:47,代码来源:HttpRequestDispatcherHandler.java


注:本文中的org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。