【问题标题】:Netty 4.x.x WebSocket handshake for streaming server用于流式服务器的 Netty 4.x.x WebSocket 握手
【发布时间】:2016-07-01 16:34:30
【问题描述】:

我有一个来自 Official Netty 的 Echo 服务器示例 Echo Server

如何添加从 websocket 连接和流式传输的功能?

这是我的 ServerHandler 代码:

public class ServerHandler extends ChannelInboundHandlerAdapter
{
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
        // !!!!! Think here should be WebSocket Handshake?
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
    {
        System.out.println(msg);
        ctx.write(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx)
    {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
    {
          // Close the connection when an exception is raised.
        cause.printStackTrace();
    }
}

现在 Chrome 连接显示:WebSocket 连接到 'ws://127.0.0.1:8080/' 失败:WebSocket 握手期间出错:状态行无效

【问题讨论】:

    标签: java websocket netty


    【解决方案1】:

    Netty 服务器不会自动处理所有协议,因此您需要添加对 WebSockets 的支持。

    我发现最好的起点是检查 Netty 的 xref 页面中的相关示例。向下滚动包列表,直到找到 io.netty.example 包。在该列表中,您将找到一个名为 io.netty.example.http.websocketx.server 的包。关于如何实现 websocket 服务器或只是处理程序,有一个相当简单且布局合理的示例。

    Websocket 服务器比其他服务器稍微复杂一些,因为它们必须以 HTTP 服务器的形式启动,因为协议规定 websocket 必须通过“升级”HTTP 连接来启动,但正如我所说,上面引用的示例使这个相当清楚。

    【讨论】:

      【解决方案2】:

      所以,我找到了解决方案!它不符合 web-socket 的本机文档,但谁在乎它按我的预期工作!

      public void channelRead(ChannelHandlerContext ctx, Object msg)
          {
              DefaultHttpRequest httpRequest = null;
              if (msg instanceof DefaultHttpRequest)
              {
                  httpRequest = (DefaultHttpRequest) msg;
      
                  // Handshake
                  WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory("ws://127.0.0.1:8080/", null, false);
                  final Channel channel = ctx.channel();
                  final WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(httpRequest);
      
                  if (handshaker == null) {
      
                  } else {
                      ChannelFuture handshake = handshaker.handshake(channel, httpRequest);
                  }
              }
          }
      

      别忘了添加

      p.addLast(new HttpRequestDecoder(4096, 8192, 8192, false));
      p.addLast(new HttpResponseEncoder());
      

      到您的管道。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-03
        • 2011-12-26
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2013-12-25
        • 1970-01-01
        • 2012-11-11
        相关资源
        最近更新 更多