This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details

1. What are the main functions of Netty

The webSocket WSSCopy the code
@override public void initChannel(SocketChannel ch) throws Exception {ChannelPipeline = ch.pipeline();  pipeline.addLast(new IdleStateHandler(10, 0, 0, TimeUnit.SECONDS)); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(1024 * 1024)); pipeline.addLast(new ChunkedWriteHandler()); // pipeline.addLast(new HttpRequestHandler("/ws")); pipeline.addLast(new WebSocketServerProtocolHandler("/ws", null, true, 1024 * 512)); // pipeline.addLast(new HeartbeatServerHandler()); pipeline.addLast(new TextWebSocketFrameHandler()); / / configure SSL access logger. The info (WebsocketChatServerInitializer. Class. GetResource ("/"). The toString ()); URL xmlpath = this.getClass().getClassLoader().getResource(""); logger.info(xmlpath.toString()); // File f = new File(this.getClass().getResource("/").getPath()); File f = new File(WebsocketChatServerInitializer.class.getResource("").toString()); logger.info(f.getAbsolutePath()); SSLContext sslContext = SslUtil.createSSLContext("PKCS12", this.getClass().getClassLoader().getResourceAsStream("keystore.p12"), "changeit"); SSLEngine engine =sslContext.createSSLEngine(); SSLEngine sslEngine = sslContext.createSSLEngine(); sslEngine.setNeedClientAuth(false); sslEngine.setUseClientMode(false); pipeline.addFirst(new SslHandler(sslEngine)); }Copy the code

2. The basic flow of WEB chat and some limitations

2.1 Message sending using WSS 2.2 First, the front-end user logs in to the server, obtains the token, and then connects webSocket to bind the server. 2.3 The server saves a key-value pair after verifying the token. User name :Channel To prevent malicious messages from being sent, the server obtains the Channel based on the user name. 2.4 After the Web server is bound, it can send and receive messages. The message format is JSON. The Netty server mainly obtains connections through the saved Channel. 2.5 Configure a background management module to manage users forcibly, and then broadcast messagesCopy the code

3. Message format

{" CMD ":null, "mdChild":ull, "from":null, "Accept ":null, "group":null," MSG ":null, "status":null, "id":null, "CreateDate ":"2021-01-09 22:26:14", "chatSet":null, "groupSet":null, "oldMsg":null} "From" : {" userCode ":" DNMT} ", "accept" : {" userCode ":" FHX} ", "MSG" : "single chat test"} {group chat "CMD" : "4", "From" : {" userCode ":" DNMT} ", "group" : {" groupCode ":" g003} ", "MSG" : "test group chat"}Copy the code

4 Extension of the server

3.1 As the number of users increases, the server may be unable to cope with it. The alternative is a server-side cluster deployment. 3.2 Cluster Deployment Problems May Messages Be Pushed across Servers? Cross-server binding for users? 3.3 Netty Cluster Solution For Cross-server Message Push The idea uses redis publishing and subscription to forward messages. The user obtains subscription messages to determine whether the user exists, and processes messages if the user exists. 3.4 Cross-server binding of users. Zookeeper is used here. When the server is started, the connection information of the server will be sent to ZooKeeper. When the user logs in, the binding information of the Netty server can be obtained according to a simple load balancing algorithm (random or minimal), and then bound to the back-end server. 3.5 Further Performance Improvement The server uses thread pools to forward messages.Copy the code

5. A place to play

1. Support WEB voice and video chat at the beginning, peerJS 'WEBRTC mode was adopted for video chat, but I did not know node and felt it was uncontrollable, so I adopted the following method. 2. Data is periodically obtained through the audio and video recording functions of WEBRTC browser, and then data is transmitted through Base64 encoding and decoded to the Web for audio and video chat. 3. In video chat, you can use OpencV to do some video operations (here is where I started learning OpencV) simple examples: The idea of face recognition in the video is to extract the audio of the video file (FFMPEG) first, and then get the picture of each frame by the processing of the video jump frame (mainly to improve the high point speed). After the processing, the extracted audio is merged with the new video file, and then encode the video stream forwardingCopy the code

6.Welcome to star at project address