gazetk / DesktopConnector / DesktopConnector.js @ 908a231e
History | View | Annotate | Download (5.648 KB)
1 |
var net = require('net'); |
---|---|
2 |
var ws = require("nodejs-websocket"); |
3 |
|
4 |
var HOST = '127.0.0.1'; |
5 |
var PORT = 6555; |
6 |
var connected = false; |
7 |
var client = new net.Socket(); |
8 |
|
9 |
var connectionManager = [];
|
10 |
var connectionId = 0; |
11 |
var websocket_server = null; |
12 |
|
13 |
var eyeTracker = ""; |
14 |
if (process.argv.length == 2) { |
15 |
|
16 |
console.log("Start the server with --tracker=smi or --tracker=mygaze or --tracker=eyetribe");
|
17 |
process.exit(); |
18 |
|
19 |
} else if (process.argv.length > 2) { |
20 |
process.argv.forEach(function(val, index, array) {
|
21 |
|
22 |
if (val.indexOf("tracker") > -1) { |
23 |
eyeTracker = val.split("=")[1]; |
24 |
} |
25 |
|
26 |
}); |
27 |
} |
28 |
|
29 |
if (eyeTracker == "" || eyeTracker == undefined) { |
30 |
console.log("tracker was not set - exiting.");
|
31 |
process.exit(); |
32 |
} |
33 |
|
34 |
console.log("Tracker is " + eyeTracker);
|
35 |
|
36 |
function createServer() { |
37 |
websocket_server = ws.createServer(function(conn) {
|
38 |
conn.on("text", function(str) { |
39 |
|
40 |
|
41 |
var senderID;
|
42 |
for (var c in connectionManager) { |
43 |
//console.log(c+"{__}"+position);
|
44 |
if (conn == connectionManager[c].conn) {
|
45 |
senderID = connectionManager[c].id; |
46 |
} |
47 |
} |
48 |
|
49 |
console.log("Message from connection with id:" + senderID + " : \"" + str + "\""); |
50 |
if (str.indexOf("TYPE") > -1) { |
51 |
console.log("typestring: " + str);
|
52 |
var clientType = str.split(':'); |
53 |
//connectionManager[connectionId] = new Array();
|
54 |
console.log("type = " + clientType[1]); |
55 |
|
56 |
for (var c in connectionManager) { |
57 |
//console.log("Sending SMI Data: "+str);
|
58 |
if (connectionManager[c]['conn'] == conn) { |
59 |
connectionManager[c]['type'] = clientType[1]; |
60 |
} |
61 |
} |
62 |
|
63 |
|
64 |
} |
65 |
|
66 |
if (eyeTracker == "smi") { |
67 |
try {
|
68 |
for (var c in connectionManager) { |
69 |
console.log("Sending SMI Data: " + str);
|
70 |
if (connectionManager[c]['type'] == "browser") { |
71 |
connectionManager[c]['conn'].sendText(str);
|
72 |
} |
73 |
} |
74 |
} catch (err) {
|
75 |
console.log("error in websocketserver: ");
|
76 |
console.log("error at line:" + err.lineNumber);
|
77 |
console.log("error:" + err);
|
78 |
console.log("error at message:" + data);
|
79 |
console.log("trying to establish new connection");
|
80 |
createServer(); |
81 |
} |
82 |
|
83 |
|
84 |
} |
85 |
if (eyeTracker == "mygaze") { |
86 |
try {
|
87 |
for (var c in connectionManager) { |
88 |
console.log("Sending myGaze Data: " + str);
|
89 |
if (connectionManager[c]['type'] == "browser") { |
90 |
connectionManager[c]['conn'].sendText(str);
|
91 |
} |
92 |
} |
93 |
} catch (err) {
|
94 |
console.log("error in websocketserver: ");
|
95 |
console.log("error at line:" + err.lineNumber);
|
96 |
console.log("error:" + err);
|
97 |
console.log("error at message:" + data);
|
98 |
console.log("trying to establish new connection");
|
99 |
createServer(); |
100 |
} |
101 |
|
102 |
|
103 |
} |
104 |
if (eyeTracker == "eyetribe") { |
105 |
//send the message from the browser directly to the eyetracker
|
106 |
client.write(str); |
107 |
} |
108 |
}); |
109 |
|
110 |
conn.on('error', function(er) { |
111 |
// The error won't crash the process, but what it does is worse!
|
112 |
// Though we've prevented abrupt process restarting, we are leaking
|
113 |
// resources like crazy if this ever happens.
|
114 |
// This is no better than process.on('uncaughtException')!
|
115 |
console.log(eyeTracker +" disconnected. ERRORMESSAGE: ", er.message);
|
116 |
}); |
117 |
|
118 |
|
119 |
conn.on("close", function(code, reason) { |
120 |
for (var c in connectionManager) { |
121 |
//console.log(c+"{__}"+position);
|
122 |
if (conn == connectionManager[c].conn) {
|
123 |
console.log("connection '" + c + "' disconnected."); |
124 |
delete connectionManager[c];
|
125 |
} |
126 |
} |
127 |
}); |
128 |
}).listen(6777);
|
129 |
|
130 |
|
131 |
|
132 |
websocket_server.on("connection", function(conn) { |
133 |
connectionManager[connectionId] = []; |
134 |
connectionManager[connectionId].conn = conn; |
135 |
connectionManager[connectionId].id = connectionId; |
136 |
console.log("A new client connected with id: " + connectionId);
|
137 |
connectionId++; |
138 |
}); |
139 |
|
140 |
} |
141 |
|
142 |
|
143 |
|
144 |
if (eyeTracker == "eyetribe") { |
145 |
//Heartbeat timer
|
146 |
setInterval(function() {
|
147 |
if (connected) {
|
148 |
sendHeartbeat(client); |
149 |
} |
150 |
}, 250);
|
151 |
client.connect(PORT, HOST, function() {
|
152 |
|
153 |
console.log('Websocket connected successfully to tcp server(EyeTracker): ' + HOST + ':' + PORT); |
154 |
connected = true;
|
155 |
|
156 |
//check the eyetracker status
|
157 |
//client.write('{"category": "tracker","request": "get","values": ["push", "iscalibrated" ]}');
|
158 |
//client.write('{"category": "tracker","request": "set","values": {"push": true,"version": 1}}');
|
159 |
|
160 |
//client.write('{"category": "tracker","request" : "get","values": [ "push", "iscalibrated" ]}');
|
161 |
}); |
162 |
} else if (eyeTracker == "smi") { |
163 |
console.log('Waiting for messages from SMI EyeTracker on 127.0.0.1:6777');
|
164 |
|
165 |
} else if (eyeTracker == "mygaze") { |
166 |
console.log('Waiting for messages from myGaze EyeTracker on 127.0.0.1:6777');
|
167 |
|
168 |
} |
169 |
|
170 |
|
171 |
//as soon as the websocket receives any data from the eyetracker it is forwarded to the browser
|
172 |
client.on('data', function(data) { |
173 |
|
174 |
try {
|
175 |
for (var c in connectionManager) { |
176 |
console.log("Sending EyeTrackingData to Browser: "+data);
|
177 |
|
178 |
connectionManager[c]['conn'].sendText(data);
|
179 |
|
180 |
} |
181 |
} catch (err) {
|
182 |
console.log("error in websocketserver: ");
|
183 |
console.log("error at line:" + err.lineNumber);
|
184 |
console.log("error:" + err);
|
185 |
console.log("error at message:" + data);
|
186 |
console.log("trying to establish new connection");
|
187 |
createServer(); |
188 |
} |
189 |
// Close the client socket completely
|
190 |
//client.destroy();
|
191 |
}); |
192 |
|
193 |
// Add a 'close' event handler for the client socket
|
194 |
client.on('close', function() { |
195 |
connected = false;
|
196 |
console.log('EyeTracker Disconnected.');
|
197 |
createServer(); |
198 |
|
199 |
}); |
200 |
|
201 |
function sendHeartbeat(socket) { |
202 |
socket.write("{\"category\":\"heartbeat\",\"request\":null}");
|
203 |
//console.log('Heartbeat sent.');
|
204 |
} |
205 |
|
206 |
|
207 |
|
208 |
try {
|
209 |
createServer(); |
210 |
} catch (err) {
|
211 |
console.log("error in websocketserver: ");
|
212 |
console.log("error at line:" + err.lineNumber);
|
213 |
console.log("error:" + err);
|
214 |
console.log("error at message:" + data);
|
215 |
console.log("trying to establish new connection");
|
216 |
createServer(); |
217 |
} |