Add a dependency to pom.xml

<dependency> <groupId>org.yeauty</groupId> <artifactId>netty-websocket-spring-boot-starter</artifactId> The < version > 0.8.0 < / version > < / dependency >Copy the code

2. Add the NETty server address and port number to the SpringBoot configuration file

Add the following code to application.yml

Netty_socket: host: 192.168.1.1 Path: port: 8080Copy the code

3. Configure the netty entity class

package cn.stylefeng.guns.core.Netty;

import io.netty.channel.ChannelId;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.timeout.IdleStateEvent;
import org.springframework.stereotype.Component;
import org.yeauty.annotation.*;
import org.yeauty.pojo.ParameterMap;
import org.yeauty.pojo.Session;

import java.io.IOException;
import java.util.HashMap;

@ServerEndpoint(prefix = "netty_socket")
@Component
public class MyWebSocket {
    public static HashMap<ChannelId, Session> nettySession=new HashMap<ChannelId, Session> ();
    @OnOpen
    public void onOpen(Session session, HttpHeaders headers, ParameterMap parameterMap) throws IOException {
        System.out.println(session.id()+"Join");
        nettySession.put(session.id(),session);
    }

    @OnClose
    public void onClose(Session session) throws IOException {
        System.out.println(session.id()+"Quit");
        nettySession.remove(session.id());
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        throwable.printStackTrace();
    }

    @OnMessage
    public void onMessage(Session session, String message) {
        session.sendText(message);
    }

    @OnBinary
    public void onBinary(Session session, byte[] bytes) {
        for (byte b : bytes) {
            System.out.println(b);
        }
        session.sendBinary(bytes);
    }

    @OnEvent
    public void onEvent(Session session, Object evt) {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
            switch (idleStateEvent.state()) {
                case READER_IDLE:
                    System.out.println("read idle");
                    break;
                case WRITER_IDLE:
                    System.out.println("write idle");
                    break;
                case ALL_IDLE:
                    System.out.println("all idle");
                    break;
                default:
                    break; }}}}Copy the code

4. Specific implementation of background code

Add it to controller or Service

MyWebSocket so = new MyWebSocket();
        for (Map.Entry<ChannelId, Session> m : so.nettySession.entrySet()) {
            so.onMessage(m.getValue(), "aaaaaaaaaaa");
        }
Copy the code

4. The concrete implementation of the foreground Websocket

var wsServer = 'the ws: / / 192.168.1.1:8080 /'; var websocket = new WebSocket(wsServer); Var socketFlag; websocket.onopen =function(evt) { socketflag = 0; }; // Listen for server data push websocket.onmessage =function (evt) {
             alert(evt.data);
        };

        function reconnect() {
            setTimeout(function () {
                websocket = new WebSocket('the ws: / / 192.168.1.58:8082 /');
                websocket.onclose = function () {
                    reconnect()
                };
                websocket.onerror = function () {
                    reconnect()
                };
                websocket.onmessage = function (evt) {
                    if(evt.data == 1) { } }; }, 2000); } // Listen on connection closed websocket.onclose =function (evt) {
            reconnect()
        };
Copy the code

The end