美文网首页
netty websocket wss服务

netty websocket wss服务

作者: cjlynn | 来源:发表于2021-01-12 11:47 被阅读0次

最简单提供wss服务的方法,就是使用netty的ws服务,而不是sslhandler,使用nginx代理wss即可。
1、netty直接提供ws的服务

    @Override
    protected void initChannel(NioSocketChannel client) throws Exception {
        ChannelPipeline pipeline = client.pipeline();
        //websocket协议本身是基于http协议的,所以这边也要使用http解编码器
        pipeline.addLast(new HttpServerCodec());
        //以块的方式来写的处理器
        pipeline.addLast(new ChunkedWriteHandler());
        pipeline.addLast(new HttpObjectAggregator(64*1024));
        pipeline.addLast(textWebSocketFrameHandler);
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws", "WebSocket", true, 65536 * 10));
    }

nginx配置wss代理

http {
    #websocket 需要加下这个
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
    server {
...
        location /ws { 
            proxy_pass http://127.0.0.1:8089;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
        }
    }
...
}

相关文章

网友评论

      本文标题:netty websocket wss服务

      本文链接:https://www.haomeiwen.com/subject/dbvsaktx.html