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