blinker / firefox.plugin / data / websocket / websockethtmlscript.js @ master
History | View | Annotate | Download (2.119 KB)
1 |
/*
|
---|---|
2 |
* Copyright 2015 Thies Pfeiffer and Dimitri Heil and Kevin Taron
|
3 |
* Blinker is distributed under the terms of the GNU General Public License
|
4 |
*
|
5 |
* This file is part of Blinker.
|
6 |
*
|
7 |
* Blinker 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 |
* Blinker 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 Blinker. If not, see <http://www.gnu.org/licenses/>.
|
19 |
*/
|
20 |
|
21 |
var websocket = new WebSocket('ws://localhost:6777'); |
22 |
websocket.onmessage = function(evt) { messageReceived(evt); }; |
23 |
websocket.onopen = function(evt) { conReady(); }; |
24 |
websocket.onerror = function(evt) { onError(evt); }; |
25 |
|
26 |
function disconnect(){ |
27 |
websocket.close(); |
28 |
} |
29 |
|
30 |
function onError(evt) { |
31 |
console.log("An error occured in the websocket.html - is the WebSocket Server running?");
|
32 |
} |
33 |
|
34 |
function messageReceived(evt) { |
35 |
|
36 |
addon.port.emit('eyeTrackerData',evt.data);
|
37 |
|
38 |
} |
39 |
|
40 |
function conReady() { |
41 |
console.log("connection established");
|
42 |
|
43 |
try{
|
44 |
sendMessage("TYPE:browser");
|
45 |
} catch (err){
|
46 |
console.log("Error in Websocket Page-Worker: "+err.name);
|
47 |
} |
48 |
|
49 |
|
50 |
console.log("adding port listener");
|
51 |
addon.port.on("toEyetracker",function (message){ |
52 |
//console.log("websocket.html sending to websocketserver: "+message);
|
53 |
try{
|
54 |
sendMessage(message); |
55 |
} catch (err){
|
56 |
console.log("Error in Websocket Page-Worker: "+err.name);
|
57 |
} |
58 |
}); |
59 |
|
60 |
addon.port.on("disconnect",function (){ |
61 |
console.log("websocket.html disconnecting: ");
|
62 |
try{
|
63 |
disconnect(); |
64 |
} catch (err){
|
65 |
console.log("Error in Websocket Page-Worker: "+err.name);
|
66 |
} |
67 |
}); |
68 |
|
69 |
addon.port.emit('conReady',"ready"); |
70 |
websocket.send("HI.");
|
71 |
} |
72 |
|
73 |
function sendMessage(message){ |
74 |
websocket.send(message); |
75 |
} |