-
Notifications
You must be signed in to change notification settings - Fork 127
/
receive.html
238 lines (218 loc) · 9.82 KB
/
receive.html
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width">
<title>Peer-to-Peer Cue System --- Reciever</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<table class="display">
<tr>
<td class="title">Status:</td>
<td class="title">Messages:</td>
</tr>
<tr>
<td>
<div id="receiver-id" style="font-weight: bold;" title="Copy this ID to the input on send.html.">ID:</div>
</td>
<td>
<input type="text" id="sendMessageBox" placeholder="Enter a message..." autofocus="true" />
<button type="button" id="sendButton">Send</button>
<button type="button" id="clearMsgsButton">Clear Msgs (Local)</button>
</td>
</tr>
<tr>
<td><div id="status" class="status"></div></td>
<td><div class="message" id="message"></div></td>
</tr>
<tr>
<td class="display-box standby" id="standby"><h2>Standby</h2></td>
<td class="display-box hidden" id="go"><h2>Go</h2></td>
</tr>
<tr>
<td class="display-box hidden" id="fade"><h2>Fade</h2></td>
<td class="display-box hidden" id="off"><h2>All Off</h2></td>
</tr>
</table>
<script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
<script type="text/javascript">
(function () {
var lastPeerId = null;
var peer = null; // Own peer object
var peerId = null;
var conn = null;
var recvId = document.getElementById("receiver-id");
var status = document.getElementById("status");
var message = document.getElementById("message");
var standbyBox = document.getElementById("standby");
var goBox = document.getElementById("go");
var fadeBox = document.getElementById("fade");
var offBox = document.getElementById("off");
var sendMessageBox = document.getElementById("sendMessageBox");
var sendButton = document.getElementById("sendButton");
var clearMsgsButton = document.getElementById("clearMsgsButton");
/**
* Create the Peer object for our end of the connection.
*
* Sets up callbacks that handle any events related to our
* peer object.
*/
function initialize() {
// Create own peer object with connection to shared PeerJS server
peer = new Peer(null, {
debug: 2
});
peer.on('open', function (id) {
// Workaround for peer.reconnect deleting previous id
if (peer.id === null) {
console.log('Received null id from peer open');
peer.id = lastPeerId;
} else {
lastPeerId = peer.id;
}
console.log('ID: ' + peer.id);
recvId.innerHTML = "ID: " + peer.id;
status.innerHTML = "Awaiting connection...";
});
peer.on('connection', function (c) {
// Allow only a single connection
if (conn && conn.open) {
c.on('open', function() {
c.send("Already connected to another client");
setTimeout(function() { c.close(); }, 500);
});
return;
}
conn = c;
console.log("Connected to: " + conn.peer);
status.innerHTML = "Connected";
ready();
});
peer.on('disconnected', function () {
status.innerHTML = "Connection lost. Please reconnect";
console.log('Connection lost. Please reconnect');
// Workaround for peer.reconnect deleting previous id
peer.id = lastPeerId;
peer._lastServerId = lastPeerId;
peer.reconnect();
});
peer.on('close', function() {
conn = null;
status.innerHTML = "Connection destroyed. Please refresh";
console.log('Connection destroyed');
});
peer.on('error', function (err) {
console.log(err);
alert('' + err);
});
};
/**
* Triggered once a connection has been achieved.
* Defines callbacks to handle incoming data and connection events.
*/
function ready() {
conn.on('data', function (data) {
console.log("Data recieved");
var cueString = "<span class=\"cueMsg\">Cue: </span>";
switch (data) {
case 'Go':
go();
addMessage(cueString + data);
break;
case 'Fade':
fade();
addMessage(cueString + data);
break;
case 'Off':
off();
addMessage(cueString + data);
break;
case 'Reset':
reset();
addMessage(cueString + data);
break;
default:
addMessage("<span class=\"peerMsg\">Peer: </span>" + data);
break;
};
});
conn.on('close', function () {
status.innerHTML = "Connection reset<br>Awaiting connection...";
conn = null;
});
}
function go() {
standbyBox.className = "display-box hidden";
goBox.className = "display-box go";
fadeBox.className = "display-box hidden";
offBox.className = "display-box hidden";
return;
};
function fade() {
standbyBox.className = "display-box hidden";
goBox.className = "display-box hidden";
fadeBox.className = "display-box fade";
offBox.className = "display-box hidden";
return;
};
function off() {
standbyBox.className = "display-box hidden";
goBox.className = "display-box hidden";
fadeBox.className = "display-box hidden";
offBox.className = "display-box off";
return;
}
function reset() {
standbyBox.className = "display-box standby";
goBox.className = "display-box hidden";
fadeBox.className = "display-box hidden";
offBox.className = "display-box hidden";
return;
};
function addMessage(msg) {
var now = new Date();
var h = now.getHours();
var m = addZero(now.getMinutes());
var s = addZero(now.getSeconds());
if (h > 12)
h -= 12;
else if (h === 0)
h = 12;
function addZero(t) {
if (t < 10)
t = "0" + t;
return t;
};
message.innerHTML = "<br><span class=\"msg-time\">" + h + ":" + m + ":" + s + "</span> - " + msg + message.innerHTML;
}
function clearMessages() {
message.innerHTML = "";
addMessage("Msgs cleared");
}
// Listen for enter in message box
sendMessageBox.addEventListener('keypress', function (e) {
var event = e || window.event;
var char = event.which || event.keyCode;
if (char == '13')
sendButton.click();
});
// Send message
sendButton.addEventListener('click', function () {
if (conn && conn.open) {
var msg = sendMessageBox.value;
sendMessageBox.value = "";
conn.send(msg);
console.log("Sent: " + msg)
addMessage("<span class=\"selfMsg\">Self: </span>" + msg);
} else {
console.log('Connection is closed');
}
});
// Clear messages box
clearMsgsButton.addEventListener('click', clearMessages);
initialize();
})();
</script>
</body>
</html>