forked from jmcker/Peer-to-Peer-Cue-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.html
255 lines (233 loc) · 10.5 KB
/
send.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width">
<title>Peer-to-Peer Cue System --- Sender</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Peer-to-Peer Cue System --- Sender</h1>
<table class="control">
<tr>
<td class="title">Status:</td>
<td class="title">Messages:</td>
</tr>
<tr>
<td>
<span style="font-weight: bold">ID: </span>
<input type="text" id="receiver-id" title="Input the ID from receive.html">
<button id="connect-button">Connect</button>
</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>
<button type="button" class="control-button" id="resetButton">Reset</button>
</td>
<td>
<button type="button" class="control-button" id="goButton">Go</button>
</td>
</tr>
<tr>
<td>
<button type="button" class="control-button" id="fadeButton">Fade</button>
</td>
<td>
<button type="button" class="control-button" id="offButton">Off</button>
</td>
</tr>
</table>
<script src="https://unpkg.com/peerjs@1.3.1/dist/peerjs.min.js"></script>
<script type="text/javascript">
(function () {
var lastPeerId = null;
var peer = null; // own peer object
var conn = null;
var recvIdInput = document.getElementById("receiver-id");
var status = document.getElementById("status");
var message = document.getElementById("message");
var goButton = document.getElementById("goButton");
var resetButton = document.getElementById("resetButton");
var fadeButton = document.getElementById("fadeButton");
var offButton = document.getElementById("offButton");
var sendMessageBox = document.getElementById("sendMessageBox");
var sendButton = document.getElementById("sendButton");
var clearMsgsButton = document.getElementById("clearMsgsButton");
var connectButton = document.getElementById("connect-button");
var cueString = "<span class=\"cueMsg\">Cue: </span>";
/**
* 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);
});
peer.on('connection', function (c) {
// Disallow incoming connections
c.on('open', function() {
c.send("Sender does not accept incoming connections");
setTimeout(function() { c.close(); }, 500);
});
});
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);
});
};
/**
* Create the connection between the two Peers.
*
* Sets up callbacks that handle any events related to the
* connection and data received on it.
*/
function join() {
// Close old connection
if (conn) {
conn.close();
}
// Create connection to destination peer specified in the input field
conn = peer.connect(recvIdInput.value, {
reliable: true
});
conn.on('open', function () {
status.innerHTML = "Connected to: " + conn.peer;
console.log("Connected to: " + conn.peer);
// Check URL params for comamnds that should be sent immediately
var command = getUrlParam("command");
if (command)
conn.send(command);
});
// Handle incoming data (messages only since this is the signal sender)
conn.on('data', function (data) {
addMessage("<span class=\"peerMsg\">Peer:</span> " + data);
});
conn.on('close', function () {
status.innerHTML = "Connection closed";
});
};
/**
* Get first "GET style" parameter from href.
* This enables delivering an initial command upon page load.
*
* Would have been easier to use location.hash.
*/
function getUrlParam(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return null;
else
return results[1];
};
/**
* Send a signal via the peer connection and add it to the log.
* This will only occur if the connection is still alive.
*/
function signal(sigName) {
if (conn && conn.open) {
conn.send(sigName);
console.log(sigName + " signal sent");
addMessage(cueString + sigName);
} else {
console.log('Connection is closed');
}
}
goButton.addEventListener('click', function () {
signal("Go");
});
resetButton.addEventListener('click', function () {
signal("Reset");
});
fadeButton.addEventListener('click', function () {
signal("Fade");
});
offButton.addEventListener('click', function () {
signal("Off");
});
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);
// Start peer connection on click
connectButton.addEventListener('click', join);
// Since all our callbacks are setup, start the process of obtaining an ID
initialize();
})();
</script>
</body>
</html>