-
Notifications
You must be signed in to change notification settings - Fork 0
/
cortex.js
62 lines (56 loc) · 1.64 KB
/
cortex.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const WebSocket = require('ws')
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
class Cortex {
constructor (user, socketUrl = 'wss://localhost:6868') {
this.socket = new WebSocket(socketUrl)
this.headsetId = user.headsetId
this.authToken = user.authToken
}
createSession (authToken, headsetId) {
const CREATE_SESSION_ID = 5
const createSessionRequest = {
jsonrpc: '2.0',
id: CREATE_SESSION_ID,
method: 'createSession',
params: {
cortexToken: authToken,
headset: headsetId,
status: 'active'
}
}
return new Promise(resolve => {
this.socket.send(JSON.stringify(createSessionRequest))
this.socket.on('message', data => {
try {
if (JSON.parse(data).id === CREATE_SESSION_ID) {
const sessionId = JSON.parse(data).result.id
resolve(sessionId)
}
} catch (error) { }
})
})
}
subRequest (stream, authToken, sessionId) {
const SUB_REQUEST_ID = 6
const subRequest = {
jsonrpc: '2.0',
method: 'subscribe',
params: {
cortexToken: authToken,
session: sessionId,
streams: stream
},
id: SUB_REQUEST_ID
}
this.socket.send(JSON.stringify(subRequest))
}
sub (streams, event) {
this.socket.on('open', async () => {
this.sessionId = await this.createSession(this.authToken, this.headsetId).then(result => result)
console.log('Session id: ', this.sessionId)
this.subRequest(streams, this.authToken, this.sessionId)
this.socket.on('message', event)
})
}
}
module.exports = (user, socketUrl) => new Cortex(user, socketUrl)