Skip to content

Commit

Permalink
fix(fe): connect to websocket immediately after app created
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Nov 22, 2020
1 parent 043b885 commit 90d1639
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
16 changes: 0 additions & 16 deletions web2/src/lib/Socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,4 @@ export default class Socket extends Listenable {
this.ws.close();
delete this.ws;
}

addListener(callback) {
const isFirstListener = !this.hasListeners();
const listenerId = super.addListener(callback);
if (isFirstListener) {
this.start();
}
return listenerId;
}

removeListener(id) {
super.removeListener(id);
if (!this.hasListeners()) {
this.stop();
}
}
}
6 changes: 5 additions & 1 deletion web2/src/socket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Socket from '@/lib/Socket';

export default new Socket(() => {
const socket = new Socket(() => {
const protocol = document.location.protocol === 'https:' ? 'wss' : 'ws';
return new WebSocket(`${protocol}://${document.location.host}/api/ws`);
});

socket.start();

export default socket;

0 comments on commit 90d1639

Please sign in to comment.