當前位置: 首頁>>代碼示例>>Java>>正文


Java WebSocketServerHandshaker.handshake方法代碼示例

本文整理匯總了Java中io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker.handshake方法的典型用法代碼示例。如果您正苦於以下問題:Java WebSocketServerHandshaker.handshake方法的具體用法?Java WebSocketServerHandshaker.handshake怎麽用?Java WebSocketServerHandshaker.handshake使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker的用法示例。


在下文中一共展示了WebSocketServerHandshaker.handshake方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: handleWebSocket

import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; //導入方法依賴的package包/類
/**
 * handle WebSocket request,then, the the RPC could happen in WebSocket.
 * 
 * @param ctx
 * @param request
 */
protected void handleWebSocket(final ChannelHandlerContext ctx, FullHttpRequest request) {
	if (logger.isDebugEnabled()) {
		logger.debug("handleWebSocket request: uri={}", request.uri());
	}
	// Handshake
	WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(request.uri(), null, true);
	WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(request);
	if (handshaker == null) {
		WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
		return;
	}
	ChannelFutureListener callback = websocketHandshakeListener(ctx, request);
	ChannelFuture future = handshaker.handshake(ctx.channel(), request);
	if (callback != null) {
		future.addListener(callback);
	}
	ChannelPipeline pipe = ctx.pipeline();
	if (pipe.get(WebsocketFrameHandler.class) == null) {
		pipe.addAfter(ctx.name(), "wsFrameHandler", new WebsocketFrameHandler(handshaker));
		ChannelHandler handlerAws = pipe.get(AwsProxyProtocolDecoder.class);
		if (handlerAws != null) {
			pipe.remove(handlerAws);
		}
		pipe.remove(ctx.name());// Remove current Handler
	}
}
 
開發者ID:houkx,項目名稱:nettythrift,代碼行數:33,代碼來源:HttpThriftBufDecoder.java

示例2: channelRead0

import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; //導入方法依賴的package包/類
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
    if (!req.decoderResult().isSuccess()) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
        return;
    }
    // Allow only GET methods.
    if (req.method() != GET) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
        return;
    }

    /**
     * 當且僅當uri為指定path的時候,進行websocket通訊的升級
     * */
    if (uri.equals(req.uri())
            //CONNECTION 字段的值為 UPGRADE, firefox上存在多個值的情況
            && req.headers().get(HttpHeaderNames.CONNECTION).contains(HttpHeaderValues.UPGRADE)
            //UPGRADE 字段的值為 WEBSOCKET
            && HttpHeaderValues.WEBSOCKET.contentEqualsIgnoreCase(req.headers().get(HttpHeaderNames.UPGRADE))
            ) {
        WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                uri, subprotocols, true, 5 * 1024 * 1024);
        WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
        if (handshaker == null) {
            //不支持的協議
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
        } else {
            //握手結束後補充如下協議
            handshaker.handshake(ctx.channel(), req);
        }
        return;
    }
    //錯誤的情況
    sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND));
}
 
開發者ID:lujianbo,項目名稱:moonlight-mqtt,代碼行數:37,代碼來源:WebSocketHandShaker.java

示例3: handleWebSocketResponse

import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; //導入方法依賴的package包/類
private void handleWebSocketResponse(ChannelHandlerContext ctx, Outgoing out) {
    WebSocketHttpResponse response = (WebSocketHttpResponse) out.message;
    WebSocketServerHandshaker handshaker = response.handshakerFactory().newHandshaker(lastRequest);

    if (handshaker == null) {
        HttpResponse res = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1,
                HttpResponseStatus.UPGRADE_REQUIRED);
        res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue());
        HttpUtil.setContentLength(res, 0);
        super.unbufferedWrite(ctx, new Outgoing(res, out.promise));
        response.subscribe(new CancelledSubscriber<>());
    } else {
        // First, insert new handlers in the chain after us for handling the websocket
        ChannelPipeline pipeline = ctx.pipeline();
        HandlerPublisher<WebSocketFrame> publisher = new HandlerPublisher<>(ctx.executor(), WebSocketFrame.class);
        HandlerSubscriber<WebSocketFrame> subscriber = new HandlerSubscriber<>(ctx.executor());
        pipeline.addAfter(ctx.executor(), ctx.name(), "websocket-subscriber", subscriber);
        pipeline.addAfter(ctx.executor(), ctx.name(), "websocket-publisher", publisher);

        // Now remove ourselves from the chain
        ctx.pipeline().remove(ctx.name());

        // Now do the handshake
        // Wrap the request in an empty request because we don't need the WebSocket handshaker ignoring the body,
        // we already have handled the body.
        handshaker.handshake(ctx.channel(), new EmptyHttpRequest(lastRequest));

        // And hook up the subscriber/publishers
        response.subscribe(subscriber);
        publisher.subscribe(response);
    }

}
 
開發者ID:playframework,項目名稱:netty-reactive-streams,代碼行數:35,代碼來源:HttpStreamsServerHandler.java

示例4: serverHandshake

import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; //導入方法依賴的package包/類
public ChannelHandler serverHandshake(ChannelHandlerContext ctx, FullHttpRequest msg)
{
    
    this.ctx = ctx;
    // Handshake
    WebSocketServerHandshaker handshaker;
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(msg), null, true);
    handshaker = wsFactory.newHandshaker(msg);
    if (handshaker == null)
    {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
    }
    else
    {
        ChannelPromise promise = ctx.channel().newPromise();
        promise.addListener(future -> {
            if (promise.isSuccess())
            {
                this.notifyOpen();
            }
            else
            {
                this.notifyError();
            }
        });
        handshaker.handshake(ctx.channel(), msg, null, promise);
    }
    
    return new MyChannelHandler((ctx1, frame) -> {
        handshaker.close(ctx1.channel(), (CloseWebSocketFrame) frame.retain());
    });
}
 
開發者ID:EricssonResearch,項目名稱:trap,代碼行數:33,代碼來源:WebSocketTransport.java

示例5: handleHttpRequest

import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; //導入方法依賴的package包/類
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
    // Handle a bad request.
    if (!req.getDecoderResult().isSuccess()) {
        sendErrorResponse(ctx, req, BAD_REQUEST);
        return;
    }
    // Allow only GET methods.
    if (req.getMethod() != GET) {
        sendErrorResponse(ctx, req, FORBIDDEN);
        return;
    }
    
    String path = req.getUri();
    System.out.println("Server => Request: " + path);
    try {
        if (path.equals("/ws")) {
            isWebSocket = true;
            String wsLocation = getWebSocketLocation(ctx, req);
            WebSocketServerHandshaker handshaker = new WebSocketServerHandshakerFactory(
                    wsLocation, null, false, 64 * 1024).newHandshaker(req);
            if (handshaker == null) {
                WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel())
                                                .addListener(ChannelFutureListener.CLOSE);
            } else {
                handshaker.handshake(ctx.channel(), req);
                // Push the initial state to the client.
                // Do it from the server thread were it's safe
                bossGroup.execute(() -> {
                    ctx.writeAndFlush(makeWebSocketEventFrame("stateChanged", lastState));
                });
                wsConnections.add(ctx.channel());
            }
        } 
        else {
            handleStaticFileRequest(ctx, req, path);
        }
    } catch (Throwable e) {
        sendErrorResponse(ctx, req, BAD_REQUEST);
    }
}
 
開發者ID:Matthias247,項目名稱:adalightserver,代碼行數:41,代碼來源:HttpServer.java

示例6: channelRead

import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; //導入方法依賴的package包/類
@Override
@SuppressWarnings("PMD.OnlyOneReturn")
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof FullHttpRequest) {
        FullHttpRequest req = (FullHttpRequest) msg;

        // Handle a bad request.
        if (!req.decoderResult().isSuccess()) {
            sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
            return;
        }

        // Allow only GET methods.
        if (req.method() != GET) {
            sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
            return;
        }

        // Handshake
        WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                getWebSocketLocation(req), null, false);
        WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
        if (handshaker == null) {
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
        } else {
            handshaker.handshake(ctx.channel(), req);
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}
 
開發者ID:kalixia,項目名稱:Grapi,代碼行數:32,代碼來源:WebSocketsServerProtocolUpdater.java


注:本文中的io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker.handshake方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。