本文介绍: 42-Netty基础-Websocket-Netty握手源码分析 – B站视频 – 很详细43-FrameDecoder源码分析SpringBoot 整合 Netty + WebsocketSpring boot 项目(二十三)——用 Netty+Websocket实现聊天室springBoot + netty搭建高性能 websocket 服务 & 性能测试(包含python 测试脚本)springBoot使用webSocket的几种方式以及在高并发出现的问题及解决SpringBoot2+Netty

42-Netty基础-Websocket-Netty握手源码分析 – B站视频 – 很详细
43-FrameDecoder源码分析

【netty专栏】 – 待学习

WebSocket协议:5分钟从入门到精通

SpringBoot 整合 Netty + Websocket

Spring boot 项目(二十三)——用 Netty+Websocket实现聊天室

springBoot + netty搭建高性能 websocket 服务 & 性能测试(包含python 测试脚本)
springBoot使用webSocket的几种方式以及在高并发出现的问题及解决

SpringBoot2+Netty+WebSocket(netty实现websocket,支持URL参数)
SpringBoot整合Netty处理WebSocket(支持url参数)
使用Netty处理WebSocket请求

SpringBoot 集成 Netty 使用WebSocket功能,并实现token校验

微服务springcloud环境下基于Netty搭建websocket集群实现服务器消息推送—-netty是yyds

SpringBoot2+Netty+WebSocket(netty实现websocket)

SpringBoot 整合 Netty 实现 WebSocket

Netty实战,Springboot + netty +websocket 实现推送消息

springboot实现webrtc

package com.zzhua.test06;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;

public class NettyWsServer {
    public static void main(String[] args) throws InterruptedException {

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup(16);

        try {

            ServerBootstrap serverBootstrap = new ServerBootstrap();

            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
                            ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
                            ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
                            WebSocketServerProtocolConfig wsServerConfig = WebSocketServerProtocolConfig.newBuilder()
                                    .websocketPath("/websocket")
                                    .maxFramePayloadLength(Integer.MAX_VALUE)
                                    .checkStartsWith(true).build();
                            ch.pipeline().addLast("websocketHandler", new WebSocketServerProtocolHandler(wsServerConfig));
                            ch.pipeline().addLast("wsTextHandler", new WsTextHandler());
                        }
                    });

            ChannelFuture channelFuture = serverBootstrap.bind(8080).sync();
            channelFuture.channel().closeFuture().sync();

        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }

    }
}

package com.zzhua.test06;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;

import java.nio.charset.StandardCharsets;

public class WsTextHandler extends SimpleChannelInboundHandler<WebSocketFrame> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame msg) throws Exception {
        System.out.println("收到消息: " + msg.content().toString(StandardCharsets.UTF_8));
        TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame();
        textWebSocketFrame.content().writeBytes("收到了".getBytes());
        ctx.channel().writeAndFlush(textWebSocketFrame);
    }
}

<script>
     var ws = new WebSocket('ws://localhost:8080/websocket')
     ws.onmessage  = msg => {
         console.log('收到消息: ' + msg.data);
     }

     ws.onclose = () => {
         console.log('关闭连接');
     }

     ws.onerror = () => {
         console.log('连接错误');
     }

     ws.onopen = function (event) {
         console.log("建立连接成功")
         ws.send('halo')
     }

     
 </script>

原文地址:https://blog.csdn.net/qq_16992475/article/details/134655559

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_898.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注