add notification on connect

This commit is contained in:
schlagmichdoch 2023-01-07 01:45:52 +01:00
parent 62b9e4e9e4
commit fe2422264a
2 changed files with 17 additions and 7 deletions

View file

@ -16,13 +16,22 @@ class ServerConnection {
if (this._isConnected() || this._isConnecting()) return; if (this._isConnected() || this._isConnecting()) return;
const ws = new WebSocket(this._endpoint()); const ws = new WebSocket(this._endpoint());
ws.binaryType = 'arraybuffer'; ws.binaryType = 'arraybuffer';
ws.onopen = _ => console.log('WS: server connected'); ws.onopen = _ => this._onOpen();
ws.onmessage = e => this._onMessage(e.data); ws.onmessage = e => this._onMessage(e.data);
ws.onclose = _ => this._onDisconnect(); ws.onclose = _ => this._onDisconnect();
ws.onerror = e => this._onError(e); ws.onerror = e => this._onError(e);
this._socket = ws; this._socket = ws;
} }
_onOpen() {
console.log('WS: server connected');
if (!this.firstConnect) {
this.firstConnect = true;
return;
}
Events.fire('ws-connected');
}
_onMessage(msg) { _onMessage(msg) {
msg = JSON.parse(msg); msg = JSON.parse(msg);
if (msg.type !== 'ping') console.log('WS:', msg); if (msg.type !== 'ping') console.log('WS:', msg);
@ -72,7 +81,7 @@ class ServerConnection {
this._socket.onclose = null; this._socket.onclose = null;
this._socket.close(); this._socket.close();
this._socket = null; this._socket = null;
Events.fire('disconnect'); Events.fire('ws-disconnect');
} }
_onDisconnect() { _onDisconnect() {
@ -80,7 +89,7 @@ class ServerConnection {
Events.fire('notify-user', 'Connection lost. Retry in 5 seconds...'); Events.fire('notify-user', 'Connection lost. Retry in 5 seconds...');
clearTimeout(this._reconnectTimer); clearTimeout(this._reconnectTimer);
this._reconnectTimer = setTimeout(this._connect, 5000); this._reconnectTimer = setTimeout(this._connect, 5000);
Events.fire('disconnect'); Events.fire('ws-disconnect');
} }
_onVisibilityChange() { _onVisibilityChange() {
@ -399,7 +408,7 @@ class PeersManager {
Events.on('send-text', e => this._onSendText(e.detail)); Events.on('send-text', e => this._onSendText(e.detail));
Events.on('peer-joined', e => this._onPeerJoined(e.detail)); Events.on('peer-joined', e => this._onPeerJoined(e.detail));
Events.on('peer-left', e => this._onPeerLeft(e.detail)); Events.on('peer-left', e => this._onPeerLeft(e.detail));
Events.on('disconnect', _ => this._clearPeers()); Events.on('ws-disconnect', _ => this._clearPeers());
} }
_onMessage(message) { _onMessage(message) {

View file

@ -23,7 +23,7 @@ class PeersUI {
Events.on('peers', e => this._onPeers(e.detail)); Events.on('peers', e => this._onPeers(e.detail));
Events.on('file-progress', e => this._onFileProgress(e.detail)); Events.on('file-progress', e => this._onFileProgress(e.detail));
Events.on('paste', e => this._onPaste(e)); Events.on('paste', e => this._onPaste(e));
Events.on('disconnect', _ => this._clearPeers()); Events.on('ws-disconnect', _ => this._clearPeers());
this.peers = {}; this.peers = {};
} }
@ -523,8 +523,9 @@ class Notifications {
class NetworkStatusUI { class NetworkStatusUI {
constructor() { constructor() {
Events.on('offline', this._showOfflineMessage); Events.on('offline', _ => this._showOfflineMessage());
Events.on('online', this._showOnlineMessage); Events.on('online', _ => this._showOnlineMessage());
Events.on('ws-connected', _ => this._showOnlineMessage());
if (!navigator.onLine) this._showOfflineMessage(); if (!navigator.onLine) this._showOfflineMessage();
} }