-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
162 lines (133 loc) · 4.25 KB
/
server.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
const http = require('http');
const url = require('url');
const fs = require("fs");
const path = require("path");
const os = require("os");
const assert = require("assert");
const express = require('express');
const WebSocket = require('ws');
const { vec2, vec3, vec4, quat, mat3, mat4 } = require("gl-matrix");
const manus = require("./index.js")
const osc = require("osc");
const openvr = require("./../node_vr/index.js")
let trackingstate = {
hmd: { pos: vec3.create(), quat: quat.create() },
trackers: [
{ pos: vec3.create(), quat: quat.create() },
{ pos: vec3.create(), quat: quat.create() },
]
}
const tmpmat = mat4.create();
try {
openvr.init(0);
} catch(e) {
throw openvr.EVRInitError[e];
}
function getTrackingData() {
//openvr.update();
// let res = openvr.waitGetPoses();
// if (res) {
// console.log(res)
// }
let trackerCount = 0;
for (let i=0; i<8; i++) {
let devclass = openvr.getTrackedDeviceClass(i)
if (openvr.ETrackedDeviceClass[devclass] == "TrackedDeviceClass_GenericTracker") {
let out = trackingstate.trackers[trackerCount]
openvr.getLastPoseForTrackedDeviceIndex(i, tmpmat)
mat4.getTranslation(out.pos, tmpmat)
mat4.getRotation(out.quat, tmpmat);
trackerCount++;
} else if (openvr.ETrackedDeviceClass[devclass] == "TrackedDeviceClass_HMD") {
let out = trackingstate.hmd
openvr.getLastPoseForTrackedDeviceIndex(i, tmpmat)
mat4.getTranslation(out.pos, tmpmat)
mat4.getRotation(out.quat, tmpmat)
}
}
// trackingstate console info
//console.log(trackingstate.trackers[0].pos[1], trackingstate.hmd.pos[1])
//console.log(state)
return trackingstate;
}
const project_path = process.cwd();
const server_path = __dirname;
const client_path = path.join(server_path, "client");
const app = express();
app.use(express.static(client_path))
app.get('/', function(req, res) {
res.sendFile(path.join(client_path, 'index.html'));
});
//app.get('*', function(req, res) { console.log(req); });
const server = http.createServer(app);
// add a websocket service to the http server:
const wss = new WebSocket.Server({
server: server,
maxPayload: 1024 * 1024,
});
function send_all_clients(msg, ignore) {
wss.clients.forEach(function each(client) {
if (client == ignore) return;
try {
client.send(msg);
} catch (e) {
console.error(e);
};
});
}
// whenever a client connects to this websocket:
let sessionId = 0;
wss.on('connection', function(ws, req) {
// do any
console.log("server received a connection");
console.log("server has "+wss.clients.size+" connected clients");
// ws.id = uuid.v4();
const id = ++sessionId;
const location = url.parse(req.url, true);
// You might use location.query.access_token to authenticate or share sessions
// or req.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
ws.on('error', function (e) {
if (e.message === "read ECONNRESET") {
// ignore this, client will still emit close event
} else {
console.error("websocket error: ", e.message);
}
});
// what to do if client disconnects?
ws.on('close', function(connection) {
console.log("connection closed");
console.log("server has "+wss.clients.size+" connected clients");
});
// respond to any messages from the client:
ws.on('message', function(msg) {
if (msg instanceof Buffer) {
// get an arraybuffer from the message:
const ab = msg.buffer.slice(msg.byteOffset,msg.byteOffset+msg.byteLength);
console.log("received arraybuffer", ab);
// as float32s:
//console.log(new Float32Array(ab));
} else {
if (msg == "getData") {
// reply:
ws.send(JSON.stringify({ cmd:"newData", state: manus.state }))
ws.send(JSON.stringify({ cmd: "trackingData", state:getTrackingData() }))
} else {
console.log("received message from client:", id, msg);
}
}
});
// // Example sending binary:
// const array = new Float32Array(5);
// for (var i = 0; i < array.length; ++i) {
// array[i] = i / 2;
// }
// ws.send(array);
//send_all_clients("hi")
});
server.listen(8080, function() {
console.log(`\n\n\n****************`);
console.log(`****************`);
console.log(`server listening`);
console.log(`client view on http://localhost:${server.address().port}/index.html\n\n`);
});