Handle disconnected peer WebSocket(Java) + STOMP + WebRTC

Tags: , , , ,



Currently, I have to involve in a project related to the P2P message call app. So generally on the client-side, I use the WebRTC’s API. And on the backend, beside letting 2 peers talk to each other, I need to handle the logic on each stage of the call, i.e. sending response messages to appropriate client. I want to use WebSocket as the signalling server for peers, and there are many stages from the beginning till the end of the call between 2 peers, for example: requesting, running, disconnecting, hanging up, etc…So I still need to open a WebSocket connection even after the signaling process to check for the disconnection, however, I have not much idea what can I do when a peer disconnected from the WebSocket, so I can still use the WebSocket to notify the other peer about it, or handling some logic when the peer disconnected such as wait for him to rejoin within a time interval, etc… Because I’m new to this calling project, so I need help.

More concisely, how can I detect when a peer is disconnected from the WebSocket server? And can you give me a plain example of this? Thanks very much!

Answer

Based on my comment, you detect when a user has disconnected by listening SessionDisconnectEvent event.

@Component
public class STOMPDisconnectEventListener implements ApplicationListener<SessionDisconnectEvent> {

    @Override
    public void onApplicationEvent(SessionDisconnectEvent event) {
        //event.getSessionId();
        // event.getUser();
    }
}

I guess you keep track of all your websocket session id. You can then determine which user’s session has been disconnected based on the session id or getUser().

You can also listen to SessionConnectEvent, SessionConnectedEvent, SessionSubscribeEvent and SessionUnsubscribeEvent events.



Source: stackoverflow