-
Notifications
You must be signed in to change notification settings - Fork 0
/
shower-server.client.js
113 lines (101 loc) · 3.63 KB
/
shower-server.client.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
/*global io:false */
(function (){
var socket = io(location.pathname.replace(/(^\/.*)\/$/, '$1')),
queryParams = (function () {
return location.search.substring(1).split('&').map(function (pair) {
return pair.split('=');
}).reduce(function (prev, current) {
prev[current[0]] = current.length === 2 ? current[1] : true;
return prev;
}, {});
})(),
master = false,
debug = function (message) {
if (queryParams.debug) {
console.log(message);
}
},
connected = false;
socket.on('connect', function () {
connected = true;
debug('connected, master=' + queryParams.master);
socket.emit('setMaster', queryParams.master, setMasterCallback);
addDisconnectButton()
});
function addEvent(elem, event, fn) {
if (elem.addEventListener) {
elem.addEventListener(event, fn, false);
} else {
elem.attachEvent('on' + event, function () {
// set the this pointer same as addEventListener when fn is called
return fn.call(elem, window.event);
});
}
}
function socketHashchangeHandler(data) {
if (connected) {
debug('RECEIVED: ' + JSON.stringify(data));
location.hash = data;
}
}
function browserHashchangeHandler() {
var dataToEmit = { hash: location.hash };
if (connected) {
debug('EMIT: ' + JSON.stringify(dataToEmit))
socket.emit('hashchange', dataToEmit);
}
}
function setMasterCallback(isClientMaster) {
master = isClientMaster;
if (master) {
debug('OK, adding event handler on hashchange');
// only master emits hashchanges
addEvent(window, 'hashchange', browserHashchangeHandler);
// emit current position
browserHashchangeHandler();
} else {
debug('Nyan, subscribing to socket messages');
// master doesn't subscribe to hashchanges
socket.on('hashchange', socketHashchangeHandler);
}
}
function addDisconnectButton() {
var button = document.createElement('div'),
initialOpacity = 0.2;
button.className = 'connectButton';
button.innerHTML = 'Disconnect';
button.style.position = 'fixed';
button.style.left = '50px';
button.style.top = '50px';
button.style.zIndex = 100000;
button.style.padding = '0.2em';
button.style.border = '1px solid black';
button.style.borderRadius = '5px';
button.style.opacity = initialOpacity;
button.style.backgroundColor = 'white';
document.body.appendChild(button);
addEvent(button, 'click', function () {
connected = !connected;
if ( ! connected) {
button.innerHTML = 'Connect';
if ( ! master) {
socket.removeAllListeners();
}
} else {
button.innerHTML = 'Disconnect';
if (master) {
// master sends update on re-connection
browserHashchangeHandler();
} else {
socket.emit('setMaster', queryParams.master, setMasterCallback);
}
}
});
addEvent(button, 'mouseover', function () {
button.style.opacity = 1;
});
addEvent(button, 'mouseout', function () {
button.style.opacity = initialOpacity;
});
}
})();