diff --git a/public/scripts/network.js b/public/scripts/network.js index d5a9199..c014a2c 100644 --- a/public/scripts/network.js +++ b/public/scripts/network.js @@ -16,13 +16,22 @@ class ServerConnection { if (this._isConnected() || this._isConnecting()) return; const ws = new WebSocket(this._endpoint()); ws.binaryType = 'arraybuffer'; - ws.onopen = _ => console.log('WS: server connected'); + ws.onopen = _ => this._onOpen(); ws.onmessage = e => this._onMessage(e.data); ws.onclose = _ => this._onDisconnect(); ws.onerror = e => this._onError(e); this._socket = ws; } + _onOpen() { + console.log('WS: server connected'); + if (!this.firstConnect) { + this.firstConnect = true; + return; + } + Events.fire('ws-connected'); + } + _onMessage(msg) { msg = JSON.parse(msg); if (msg.type !== 'ping') console.log('WS:', msg); @@ -72,7 +81,7 @@ class ServerConnection { this._socket.onclose = null; this._socket.close(); this._socket = null; - Events.fire('disconnect'); + Events.fire('ws-disconnect'); } _onDisconnect() { @@ -80,7 +89,7 @@ class ServerConnection { Events.fire('notify-user', 'Connection lost. Retry in 5 seconds...'); clearTimeout(this._reconnectTimer); this._reconnectTimer = setTimeout(this._connect, 5000); - Events.fire('disconnect'); + Events.fire('ws-disconnect'); } _onVisibilityChange() { @@ -399,7 +408,7 @@ class PeersManager { Events.on('send-text', e => this._onSendText(e.detail)); Events.on('peer-joined', e => this._onPeerJoined(e.detail)); Events.on('peer-left', e => this._onPeerLeft(e.detail)); - Events.on('disconnect', _ => this._clearPeers()); + Events.on('ws-disconnect', _ => this._clearPeers()); } _onMessage(message) { diff --git a/public/scripts/ui.js b/public/scripts/ui.js index 4fc01a1..bde0a52 100644 --- a/public/scripts/ui.js +++ b/public/scripts/ui.js @@ -23,7 +23,7 @@ class PeersUI { Events.on('peers', e => this._onPeers(e.detail)); Events.on('file-progress', e => this._onFileProgress(e.detail)); Events.on('paste', e => this._onPaste(e)); - Events.on('disconnect', _ => this._clearPeers()); + Events.on('ws-disconnect', _ => this._clearPeers()); this.peers = {}; } @@ -523,8 +523,9 @@ class Notifications { class NetworkStatusUI { constructor() { - Events.on('offline', this._showOfflineMessage); - Events.on('online', this._showOnlineMessage); + Events.on('offline', _ => this._showOfflineMessage()); + Events.on('online', _ => this._showOnlineMessage()); + Events.on('ws-connected', _ => this._showOnlineMessage()); if (!navigator.onLine) this._showOfflineMessage(); }