Instant Messaging with code

Code address: https://github.com/Xavier-777/Instant-Messaging


Introduction

Using Sparkjava as the back-end server, an instant chat room developed can send text messages, pictures, audios, videos and other information, send zip, docx and other files, and even perform functions such as voice and video calls.


Introduction to Sparkjava

Sparkjava here is a micro web framework, which is completely different from the big data spark. If you have knowledge of Node.js, then sparkjava is easy for you to get started, because the writing methods of the two are very similar, you can understand it as the Java version of Node.js, but Node.js is an asynchronous non-blocking framework. Sparkjava is synchronously blocking. (Actually, the teacher asked to use this framework!)


video call

The SDK implementation of agora.io (sound network) is used here, of course you can also use other SDKs


Business process introduction of project function

1. Text message sending

Websocket link creation process:
Insert picture description here

websocket messaging
Insert picture description here


2. Sending of binary files such as video and audio

Binary files are transferred by file upload, without the websocket process
Insert picture description here


3. Online video call

Here we use the sdk of Soundnet to achieve this function, the specific reference document: https://docs.agora.io/cn/Video/landing-page?platform=Web
In my project, I need to write my own appId and appCertificate In order to use the video call function, it is recommended to go to the official website to apply for one. For more details, you can go to the official document


4. Heartbeat mechanism

First understand what the heartbeat mechanism is.
The reason why it is called the heartbeat mechanism is because it sends a message to the server at regular intervals like a heartbeat to tell the server that the client is still alive. There are no fixed requirements for this information.

So now there are two situations:
1. The server does not respond, and the front-end cannot receive the message from the back-end.
2. The server responded, but the front end did not receive the information.

For situation 1: The most likely reason is that the server is down and cannot respond, the websocket is disconnected, and resources are released.
Situation 2: The server is not down, but the web page is closed, so the information is not received, the websocket is disconnected, and the resources are released.


Therefore, to achieve heartbeat detection, the server must respond to the heartbeat information, and the client must also receive the information.
Here is my implementation method:
front-end:

// 心跳检测包
var heartCheck = {
    
    
    timeout: 30000,        //30秒发一次心跳
    timeoutObj: null,
    serverTimeoutObj: null,
    reset: function(){
    
    
        clearTimeout(this.timeoutObj);
        clearTimeout(this.serverTimeoutObj);
        this.start();
    },
    start: function(){
    
    
        var self = this;
        this.timeoutObj = setTimeout(function(){
    
    
            //这里发送一个心跳,后端收到后,返回一个心跳消息,
            //onmessage拿到返回的心跳就说明连接正常
            ws.send("ping");
            self.serverTimeoutObj = setTimeout(function(){
    
    //如果超过一定时间还没重置,说明后端主动断开了
                ws.close();     //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
            }, self.timeout)
        }, this.timeout)
    }
}


webSocket.onopen = function () {
    
    
    heartCheck.reset();  //心跳检测
};
webSocket.onmessage = function (msg) {
    
    
    var data = JSON.parse(msg.data);
    if (data.heartCheck) {
    
    
        console.log(data); //获取心跳json
    } else {
    
    
        updateChat(data);//解析json,并更新聊天列表
    }
    heartCheck.reset(); //心跳检测
};

console.log(data); Print out the heartbeat detection information to prove that the client is still there.


rear end:

@OnWebSocketMessage
public void onMessage(Session user, String message) throws IOException {
    
    
    if (message.equals(heart)) {
    
    
        broadcastHeartCheck();
    } else {
    
    
        // 广播字符串
        broadcastMessage(Main.userUsernameMap.get(user), message);
    }
}

broadcastHeartCheck(); After the received message is a heart, respond with the corresponding heartbeat information to prove that the server is still there.


Unimplemented function

1. Online status confirmation and offline message processing

I went to understand the implementation mechanism and found that redis cache database is needed to record the user's status. When the user is online, send the information directly to the user; when the user is not online, save the information in the database, wait until the user is online, then send the information to the user, and then change the database record to the read state. See the reference resources below for more details.


2. Online recording of video and voice

I haven't understood it yet. I guess it can be achieved with the sdk of Shengwang, but I haven't read the official documents carefully so I dare not confirm. Or use other sdk.


to sum up

The functions that have not yet been implemented are beyond the scope of my current capabilities. I hope that dalao can help me achieve them, or wait for my ability to improve, and then slowly implement these functions.


Reference material:
Offline communication mechanism: https://zhuanlan.zhihu.com/p/42982409
Bloom filter: https://zhuanlan.zhihu.com/p/43263751

Guess you like

Origin blog.csdn.net/lendsomething/article/details/115297139