-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (59 loc) · 2.38 KB
/
index.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
63
64
65
66
67
68
69
window.onload = function () {
var app = new Vue({
el: '#app',
mounted () {
this.connectSteamVR()
},
data: function () {
return {
wsUri: 'ws://127.0.0.1:8998/',
wsData: null,
controllers: { left: {}, right: {} }
}
},
watch: {
controllers: function (newVal, oldVal) {
console.log('Update')
}
},
methods: {
connectSteamVR: function () {
console.log('Open data socket')
this.wsData = new WebSocket(this.wsUri)
this.wsData.onopen = this.onOpen
this.wsData.onclose = this.onClose
this.wsData.onerror = this.onError
this.wsData.onmessage = this.onMessage
},
onOpen: function (evt) {
console.log("Connected")
this.wsData.send('mailbox_open controller_visualizer')
this.wsData.send('mailbox_send input_server {"type":"request_input_state_updates","device_path":"/user/hand/left","returnAddress":"controller_visualizer"}')
this.wsData.send('mailbox_send input_server {"type":"request_input_state_updates","device_path":"/user/hand/right","returnAddress":"controller_visualizer"}')
},
onClose: function () {
console.log("Disconnected")
},
onError: function (evt) {
console.log('Error:', evt.data)
},
onMessage: function (evt) {
let msgData = JSON.parse(evt.data)
if (typeof msgData.components === 'undefined') return
for (const componentData in msgData.components) {
if (!msgData.components.hasOwnProperty(componentData)) continue
const componentValue = msgData.components[componentData]
let componentName = componentData.split('/')[2]
let valueType = componentData.split('/')[3]
console.log(componentData.split('/'))
let hand = null
if (msgData.device === '/user/hand/left') hand = 'left'
else if (msgData.device === '/user/hand/right') hand = 'right'
if (typeof this.controllers[hand][componentName] === 'undefined') this.controllers[hand][componentName] = {}
if (typeof this.controllers[hand][componentName][valueType] === 'undefined') this.controllers[hand][componentName][valueType] = 0
this.controllers[hand][componentName][valueType] = componentValue
}
}
}
})
}