-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWebSocket.js
More file actions
29 lines (24 loc) · 843 Bytes
/
WebSocket.js
File metadata and controls
29 lines (24 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import _ from 'lodash'
const { MESSAGE_TYPES } = require('../../server/constants')
export default class WebSocket {
connection = null
constructor(url, { onConnection, onLogUpdate, onAppListUpdate } = {}) {
this.onConnection = onConnection || _.noop
this.onLogUpdate = onLogUpdate || _.noop
this.onAppListUpdate = onAppListUpdate || _.noop
this.connection = new window.WebSocket(url)
this.connection.onmessage = this.handleMessageReceive
}
handleMessageReceive = ({ data }) => {
const { type, message } = JSON.parse(data)
switch (type) {
case MESSAGE_TYPES.CONNECTION:
return this.onConnection(message)
case MESSAGE_TYPES.LOG:
return this.onLogUpdate(message)
case MESSAGE_TYPES.APPS_LIST_UPDATE:
return this.onAppListUpdate(message)
default:
}
}
}