Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const socket = new class {
- constructor() {
- this.socketURI = '';
- this.connect();
- }
- connect() {
- this.ws = new WebSocket(this.socketURI);
- this.ws.onopen = event => this.onopen(event);
- this.ws.onclose = event => this.onclose(event);
- this.ws.onerror = event => this.onerror(event);
- this.ws.onmessage = event => this.onmessage(event);
- }
- onmessage(ev) {
- const data = JSON.parse(ev.data);
- switch(data.event) {
- // handle
- }
- }
- onopen() {
- console.log('Socket connected');
- }
- onclose() {
- console.log('Socket closed');
- setTimeout(() => this.connect(), 2000);
- }
- onerror(error) {
- console.log('Socket error: ', error);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement