This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeInit.js
123 lines (100 loc) · 3.14 KB
/
nodeInit.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
/*
initiation stuff - start a socket.io session and get a participant id
also contains code to update page content dynamically
*/
// load a javascript file dynamically
function loadJSFile(filename){
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
document.getElementsByTagName("head")[0].appendChild(fileref)
}
var pid = null; // participant id
var sid = null; // session id
var oldsocketid = null; // the last socket id we had, so we can rejoin
var stopLogging; // so we can stop continuous logging operations
var connected = false; // is the socket connection up?
var cache = []; // to collect data when no connection
// nodejs initiation
var urlarr = window.location.href.split("/");
var server = urlarr[0] + "//" + urlarr[2]
var socket = io.connect(server);
socket.on('connect', function(client){
console.log('connected ');
if(oldsocketid == null){
oldsocketid = socket.io.engine.id;
}
connected = true;
});
socket.on('disconnect', function(){
console.log("no connection");
connected = false;
});
socket.on('reconnect', function(){
console.log('reconnected');
connected = true;
if(oldsocketid != null){
socket.emit('rejoin', { "pid": pid, "session": sid, "oldid": oldsocketid });
oldsocketid = socket.id;
}
while(cache.length > 0){
console.log('sending cached data');
var cacheItem = cache.shift();
socket.emit(cacheItem[0], cacheItem[1]);
}
});
socket.on('vibrate', function(){
vibrate();
});
var nodejs = true;
var positions = [[0,0,0]]; // store gps locations
updateLocation();
// allow the content of this page to be changed
socket.on('static', function (data) {
// stop any ongoing logging
stopLogging = true;
// update content
var body = document.getElementById("content");
body.innerHTML = data.content;
// then get js to run...
var scripts = data.scripts;
for(var i = 0; i < scripts.length; i++){
loadJSFile(scripts[i]);
}
// set title
document.title = data.title + pid;
});
// send socket data, or cache it
// also adds postion information
function sendSocketData(message, data){
data["location"] = [positions[0][1], positions[0][2]];
if(connected){
socket.emit(message, data);
}
else{
// cache data
var cacheItem = [message, data];
cache.push(cacheItem);
}
}
// send pid and guid to server, and which page we want to go to
function start(destination){
pid = document.getElementById('participantid').value;
document.title += " " + pid;
sid = document.getElementById('sessions').value;
socket.emit('start', { "pid": pid, "session": sid, "url": destination });
}
function updateLocation(){
navigator.geolocation.getCurrentPosition(setLocation);
setTimeout(updateLocation, 10000);
}
function setLocation(pos) {
var now = new Date();
positions.unshift([now.getTime(), pos.coords.latitude, pos.coords.longitude]);
}
function vibrate(){
console.log('vibrating');
if (navigator.vibrate) {
navigator.vibrate(1000);
}
}