Websockets typically maintains a connection for a long period using messaging. In our case, we can call it "ping => pong", the client sends the message "ping", and the server can respond with the message "pong".
You can send ping every 30 seconds as follows.
setInterval(() => {
this.socketService.send('ping');
}, 30000);
, WebSocketService JSON,
JSON Parsing.
export class WebSocketService {
.
.
.
public connect(socketUrl) {
this.messages = websocketConnect(
socketUrl,
this.inputStream = new QueueingSubject<string>()
).messages.retryWhen(errors => errors.delay(1000))
.map(message => message === 'pong' ? message : JSON.parse(message))
.share();
}
.
.
.
}