如何在Netty 3.3中配置WebSockets(RFC 6455)?

时间:2012-01-26 19:55:13

标签: websocket netty

我有来自https://github.com/netty/netty/tree/3.2/src/main/java/org/jboss/netty/example/http/websocketx/server

的WebSockets的Netty 3.3服务器

我有客户端实现RFC 6455,但默认情况下服务器无法解码它的消息。在调试时,我看到使用了WebSocket08FrameDecoder(而不是WebSocket13FrameDecoder)。当我将客户降级到draft00时,一切正常。 如何配置Netty以解码RFC 6455消息?

更新

客户端发送此握手包:

GET /websocket HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: 5a087
Host: 127.0.0.1
Origin: 127.0.0.1

服务器上的握手是WebSocketServerHandshaker13,但我仍然收到错误:

org.jboss.netty.handler.codec.frame.CorruptedFrameException: unmasked client to server frame
at org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder.protocolViolation(WebSocket08FrameDecoder.java:350)
at org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder.decode(WebSocket08FrameDecoder.java:138)
at org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder.decode(WebSocket08FrameDecoder.java:56)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:465)
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:438)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:343)
at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:274)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:194)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)`

1 个答案:

答案 0 :(得分:2)

如果您查看WebSocketServerHandshakerFactory,它会根据HTTP标头中传递的Web套接字版本实例化解码器。

 public WebSocketServerHandshaker newHandshaker(HttpRequest req) {

    String version = req.getHeader(Names.SEC_WEBSOCKET_VERSION);
    if (version != null) {
        if (version.equals(WebSocketVersion.V13.toHttpHeaderValue())) {
            // Version 13 of the wire protocol - RFC 6455 (version 17 of the draft hybi specification).
            return new WebSocketServerHandshaker13(webSocketURL, subprotocols, allowExtensions);
        } else if (version.equals(WebSocketVersion.V08.toHttpHeaderValue())) {
            // Version 8 of the wire protocol - version 10 of the draft hybi specification.
            return new WebSocketServerHandshaker08(webSocketURL, subprotocols, allowExtensions);
        } else {
            return null;
        }
    } else {
        // Assume version 00 where version header was not specified
        return new WebSocketServerHandshaker00(webSocketURL, subprotocols);
    }
}

请检查您的客户端实现,以查看它在HTTP标头中发送的版本。它应该是Sec-WebSocket-Version: 13