current position:Home>Springboot and netty integrated chat room -- group chat

Springboot and netty integrated chat room -- group chat

2022-01-26 22:08:57 m0_ fifty-two million four hundred and eighty-seven thousand se

One 、 Create project

file-new-project-spring initializr-next
 Insert picture description here
then
 Insert picture description here
Add these two dependencies
 Insert picture description here
 Insert picture description here

Two 、 Code

DemoApplication

package com.example.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;


@SpringBootApplication
public class DemoApplication {
    

    public static void main(String[] args) throws UnknownHostException {
    
        ConfigurableApplicationContext application = SpringApplication.run(DemoApplication.class, args);
        Environment env = application.getEnvironment();
        String host = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        System.out.println("[----------------------------------------------------------]");
        System.out.println(" Chat room started successfully ! Click to enter :\t http://" + host + ":" + port);
        System.out.println("[----------------------------------------------------------");
        WebSocketServer.inst().run(53134);
    }

}

SessionGroup

package com.example.demo;

import com.google.gson.Gson;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.ChannelGroupFuture;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.ImmediateEventExecutor;
import org.springframework.util.StringUtils;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class SessionGroup {
    
    private static SessionGroup singleInstance = new SessionGroup();

    //  Mapping of groups 
    private ConcurrentHashMap<String, ChannelGroup> groupMap = new ConcurrentHashMap<>();

    public static SessionGroup inst() {
    
        return singleInstance;
    }

    public void shutdownGracefully() {
    

        Iterator<ChannelGroup> groupIterator = groupMap.values().iterator();
        while (groupIterator.hasNext()) {
    
            ChannelGroup group = groupIterator.next();
            group.close();
        }
    }

    public void sendToOthers(Map<String, String> result, SocketSession s) {
    
        //  Get group 
        ChannelGroup group = groupMap.get(s.getGroup());
        if (null == group) {
    
            return;
        }
        Gson gson=new Gson();
        String json = gson.toJson(result);
        //  Messages sent by yourself are not returned to yourself 
// Channel channel = s.getChannel();
        //  Remove channel from group 
// group.remove(channel);
        ChannelGroupFuture future = group.writeAndFlush(new TextWebSocketFrame(json));
        future.addListener(f -> {
    
            System.out.println(" Completion of transmission :"+json);
// group.add(channel);// After sending the message, add again .

        });
    }

    public void addSession(SocketSession session) {
    

        String groupName = session.getGroup();
        if (StringUtils.isEmpty(groupName)) {
    
            //  Group is empty , Go straight back to 
            return;
        }
        ChannelGroup group = groupMap.get(groupName);
        if (null == group) {
    
            group = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE);
            groupMap.put(groupName, group);
        }
        group.add(session.getChannel());
    }

    /** *  Close the connection ,  Send a notification message before closing  */
    public void closeSession(SocketSession session, String echo) {
    
        ChannelFuture sendFuture = session.getChannel().writeAndFlush(new TextWebSocketFrame(echo));
        sendFuture.addListener(new ChannelFutureListener() {
    
            public void operationComplete(ChannelFuture future) {
    
                System.out.println(" Close the connection :"+echo);
                future.channel().close();
            }
        });
    }

    /** *  Close the connection  */
    public void closeSession(SocketSession session) {
    

        ChannelFuture sendFuture = session.getChannel().close();
        sendFuture.addListener(new ChannelFutureListener() {
    
            public void operationComplete(ChannelFuture future) {
    
                System.out.println(" Send all completed :"+session.getUser().getNickname());
            }
        });

    }

    /** *  Send a message  * @param ctx  Context  * @param msg  Messages to be sent  */
    public void sendMsg(ChannelHandlerContext ctx, String msg) {
    
        ChannelFuture sendFuture = ctx.writeAndFlush(new TextWebSocketFrame(msg));
        sendFuture.addListener(f -> {
    // Send monitor 
            System.out.println(" For all send complete :"+msg);
        });
    }
}

SocketSession

package com.example.demo;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


public class SocketSession {
    
    public static final AttributeKey<SocketSession> SESSION_KEY = AttributeKey.valueOf("SESSION_KEY");

    /** *  The user realizes the core of server session management  */
//  passageway 
    private Channel channel;
    //  user 
    private User user;

    // session Unique mark 
    private final String sessionId;

    private String group;

    /** * session Stored in the session  Variable attribute value  */
    private Map<String, Object> map = new HashMap<String, Object>();

    public SocketSession(Channel channel) {
    // Note the passed in parameters channel. Different clients have different channel
        this.channel = channel;
        this.sessionId = buildNewSessionId();
        channel.attr(SocketSession.SESSION_KEY).set(this);
    }

    //  Reverse navigation 
    public static SocketSession getSession(ChannelHandlerContext ctx) {
    // Be careful ctx, Different clients have different ctx
        Channel channel = ctx.channel();
        return channel.attr(SocketSession.SESSION_KEY).get();
    }

    //  Reverse navigation 
    public static SocketSession getSession(Channel channel) {
    
        return channel.attr(SocketSession.SESSION_KEY).get();
    }

    public String getId() {
    
        return sessionId;
    }

    private static String buildNewSessionId() {
    
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("-", "");
    }

    public synchronized void set(String key, Object value) {
    
        map.put(key, value);
    }

    public synchronized <T> T get(String key) {
    
        return (T) map.get(key);
    }

    public boolean isValid() {
    
        return getUser() != null ? true : false;
    }

    public User getUser() {
    
        return user;
    }

    public void setUser(User user) {
    
        this.user = user;
    }

    public String getGroup() {
    
        return group;
    }

    public void setGroup(String group) {
    
        this.group = group;
    }

    public Channel getChannel() {
    
        return channel;
    }

}

User

package com.example.demo;

import java.util.Objects;

public class User {
    
    public String id;
    public String nickname;

    public User(String id, String nickname) {
    
        super();
        this.id = id;
        this.nickname = nickname;
    }

    public String getId() {
    
        return id;
    }

    public void setId(String id) {
    
        this.id = id;
    }

    public String getNickname() {
    
        return nickname;
    }

    public void setNickname(String nickname) {
    
        this.nickname = nickname;
    }

    @Override
    public boolean equals(Object o) {
    
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        User user = (User) o;
        return id.equals(user.getId());
    }

    @Override
    public int hashCode() {
    

        return Objects.hash(id);
    }

    public String getUid() {
    

        return id;
    }

}

WebSocketServer

package com.example.demo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
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.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class WebSocketServer {
    
    private static WebSocketServer wbss;

    private static final int READ_IDLE_TIME_OUT = 60; //  Read timeout 
    private static final int WRITE_IDLE_TIME_OUT = 0;//  Write timeout 
    private static final int ALL_IDLE_TIME_OUT = 0; //  All overtime 

    public static WebSocketServer inst() {
    
        return wbss = new WebSocketServer();
    }

    public void run(int port) {
    
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
    
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
    
                        ChannelPipeline pipeline = ch.pipeline();
                        // Netty Their own http Decoder and encoder , Message level  HTTP Decoding and encoding of the request 
                        pipeline.addLast(new HttpServerCodec());
                        // ChunkedWriteHandler  It is used for partition transmission of big data 
                        //  It is mainly used to process large data streams , For example, a 1G If you transfer files of different sizes directly, they will burst jvm In memory ;
                        //  After adding, you don't have to consider this problem 
                        pipeline.addLast(new ChunkedWriteHandler());
                        // HttpObjectAggregator  Is a complete parsing Http For message body request 
                        //  Convert multiple messages into a single complete message FullHttpRequest or FullHttpResponse,
                        //  as a result of HTTP The decoder will be in every HTTP Multiple message objects are generated in the message HttpRequest/HttpResponse,HttpContent,LastHttpContent
                        pipeline.addLast(new HttpObjectAggregator(64 * 1024));
                        // WebSocket data compression 
                        pipeline.addLast(new WebSocketServerCompressionHandler());
                        // WebSocketServerProtocolHandler It's configuration websocket The monitoring address of / Protocol packet length limit 
                        pipeline.addLast(new WebSocketServerProtocolHandler("/ws", null, true, 10 * 1024));

                        //  When connected to 60 When no message is received within seconds , Will trigger a  IdleStateEvent  event ,
                        //  This event was  HeartbeatHandler  Of  userEventTriggered  Method to 
                        pipeline.addLast(
                                new IdleStateHandler(READ_IDLE_TIME_OUT, WRITE_IDLE_TIME_OUT, ALL_IDLE_TIME_OUT, TimeUnit.SECONDS));

                        // WebSocketServerHandler、TextWebSocketFrameHandler  Is a custom logical processor ,
                        pipeline.addLast(new WebSocketTextHandler());
                    }
                });
        Channel ch = b.bind(port).syncUninterruptibly().channel();
        ch.closeFuture().syncUninterruptibly();

        //  Return to current Java Runtime objects associated with the application 
        Runtime.getRuntime().addShutdownHook(new Thread() {
    
            @Override
            public void run() {
    
                SessionGroup.inst().shutdownGracefully();
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        });
    }
}

WebSocketTextHandler

package com.example.demo;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

import java.util.HashMap;
import java.util.Map;

import static com.fasterxml.jackson.databind.type.LogicalType.Map;

public class WebSocketTextHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
    

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
    
        SocketSession session = SocketSession.getSession(ctx);
        TypeToken<HashMap<String, String>> typeToken = new TypeToken<HashMap<String, String>>() {
    
        };

        Gson gson=new Gson();
        java.util.Map<String,String> map = gson.fromJson(msg.text(), typeToken.getType());
        User user = null;
        switch (map.get("type")) {
    
            case "msg":
                Map<String, String> result = new HashMap<>();
                user = session.getUser();
                result.put("type", "msg");
                result.put("msg", map.get("msg"));
                result.put("sendUser", user.getNickname());
                SessionGroup.inst().sendToOthers(result, session);
                break;
            case "init":
                String room = map.get("room");
                session.setGroup(room);
                String nick = map.get("nick");
                user = new User(session.getId(), nick);
                session.setUser(user);
                SessionGroup.inst().addSession(session);
                break;
        }
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    

        //  Whether the handshake is successful , Upgrade to  Websocket  agreement 
        if (evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {
    
            //  Shake hands with success , remove  HttpRequestHandler, Therefore, no message will be received 
            //  And shake hands successfully  Channel  Add to  ChannelGroup  in 
            new SocketSession(ctx.channel());
        } else if (evt instanceof IdleStateEvent) {
    
            IdleStateEvent stateEvent = (IdleStateEvent) evt;
            if (stateEvent.state() == IdleState.READER_IDLE) {
    
                System.out.println("bb22");
            }
        } else {
    
            super.userEventTriggered(ctx, evt);
        }
    }
}

3、 ... and 、 function

Create a new one test.html file , Write the following code

<!DOCTYPE html>
<html lang="en">
<!DOCTYPE HTML>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title> Group chat room </title>
    <style type="text/css">
        body {
    
            margin-right:50px;
            margin-left:50px;
        }
        .ddois {
    
            position: fixed;
            left: 120px;
            bottom: 30px;
        }
    </style>
</head>
<body>
 Group name :<input type="text" id="room" name="group" placeholder=" Please enter a group ">
<br /><br />
 nickname :<input type="text" id="nick" name="name" placeholder=" Please enter your nickname ">
<br /><br />
<button type="button" onclick="enter()"> Join the chat group </button>
<br /><br />
<div id="message"></div>
<br /><br />
<div class="ddois">
    <textarea name="send" id="text" rows="10" cols="30" placeholder=" Enter send message "></textarea>
    <br /><br />
    <button type="button" onclick="send()"> send out </button>
</div>
<script type="text/javascript">
    var webSocket;

    if (window.WebSocket) {
    
        webSocket = new WebSocket("ws://localhost:53134/ws");
    } else {
    
        alert(" I'm sorry , Your browser does not support it WebSocket agreement !");
    }

    // Callback events after connectivity 
    webSocket.onopen = function() {
    
        console.log(" It's connected websocket");
// setMessageInnerHTML(" It's connected websocket");
    };
    // Callback method for connection error 
    webSocket.onerror = function(event){
    
        console.log(" Something went wrong ");
// setMessageInnerHTML(" The connection fails ");
    };

    // Call-back method for connection closure 
    webSocket.onclose = function(){
    
        console.log(" Connection closed ...");

    }

    // The callback method that receives the message 
    webSocket.onmessage = function(event){
    
        console.log("bbdds");
        var data = JSON.parse(event.data)
        var msg = data.msg;
        var nick = data.sendUser;
        switch(data.type){
    
            case 'init':
                console.log("mmll");
                break;
            case 'msg':
                console.log("bblld");
                setMessageInnerHTML(nick+": "+msg);
                break;
            default:
                break;
        }
    }
    function enter(){
    
        var map = new Map();
        var nick=document.getElementById('nick').value;
        var room=document.getElementById('room').value;
        map.set("type","init");
        map.set("nick",nick);
        console.log(room);
        map.set("room",room);
        var message = Map2Json(map);
        webSocket.send(message);
    }

    function send() {
    
        var msg = document.getElementById('text').value;
        var nick = document.getElementById('nick').value;
        console.log("1:"+msg);
        if (msg != null && msg != ""){
    
            var map = new Map();
            map.set("type","msg");
            map.set("msg",msg);
            var map2json=Map2Json(map);
            if (map2json.length < 8000){
    
                console.log("4:"+map2json);
                webSocket.send(map2json);
            }else {
    
                console.log(" The text is too long , Write less ");
            }
        }
    }

    // Displays the message on the web page 
    function setMessageInnerHTML(innerHTML) {
    
        document.getElementById("message").innerHTML += innerHTML + "<br/>";
    }

    function Map2Json(map) {
    
        var str = "{";
        map.forEach(function (value, key) {
    
            str += '"'+key+'"'+':'+ '"'+value+'",';
        })
        str = str.substring(0,str.length-1)
        str +="}";
        return str;
    }

</script>

</body>
</html>

then file–>setting–>maven–>Runner, Check this
 Insert picture description here
And then run
 Insert picture description here

copyright notice
author[m0_ fifty-two million four hundred and eighty-seven thousand se],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262208556937.html

Random recommended