Skip to content

Commit b559eea

Browse files
committed
Modernize client.js javascript
1 parent ae5f4da commit b559eea

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

client/index.html

-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ <h2>Send Text Frame</h2>
2121
<input id="data" placeholder="data" type="text" />
2222
<button id="send">Send Text</button>
2323

24-
<h2>Send Binary Frame</h2>
25-
<form name="binaryFrame" action="#">
26-
<input type="file" name="file" id="file">
27-
<button id="sendfile">Send Binary</button>
28-
</form>
29-
3024
<h2>Server-Response</h2>
3125
<div id="log"></div>
3226
</div>

client/js/client.js

+35-31
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,54 @@
11
(function() {
2-
$(document).ready(function() {
3-
var log, serverUrl, socket;
4-
log = function(msg) {
5-
return $('#log').append(`${msg}<br />`);
2+
document.addEventListener("DOMContentLoaded", function() {
3+
4+
const elLog = document.getElementById('log');
5+
const elStatus = document.getElementById('status');
6+
const elSend = document.getElementById('send');
7+
const elAction = document.getElementById('action');
8+
const elData = document.getElementById('data');
9+
10+
const log = (msg) => {
11+
return elLog.insertAdjacentHTML('afterbegin', `${msg}<br />`);
612
};
7-
serverUrl = 'ws://127.0.0.1:8000/demo';
13+
14+
let socket = null;
15+
let serverUrl = 'ws://127.0.0.1:8000/demo';
816
if (window.MozWebSocket) {
917
socket = new MozWebSocket(serverUrl);
1018
} else if (window.WebSocket) {
1119
socket = new WebSocket(serverUrl);
1220
}
1321
socket.binaryType = 'blob';
14-
socket.onopen = function(msg) {
15-
return $('#status').removeClass().addClass('online').html('connected');
22+
socket.onopen = (msg) => {
23+
elStatus.classList.remove('offline');
24+
elStatus.classList.add('online');
25+
return elStatus.innerText = 'connected';
1626
};
17-
socket.onmessage = function(msg) {
18-
var response;
19-
response = JSON.parse(msg.data);
27+
28+
socket.onmessage = (msg) => {
29+
let response = JSON.parse(msg.data);
2030
log(`Action: ${response.action}`);
2131
return log(`Data: ${response.data}`);
2232
};
23-
socket.onclose = function(msg) {
24-
return $('#status').removeClass().addClass('offline').html('disconnected');
33+
34+
socket.onclose = (msg) => {
35+
elStatus.classList.remove('online');
36+
elStatus.classList.add('offline');
37+
return elStatus.innerText = 'disconnected';
2538
};
26-
$('#status').click(function() {
39+
40+
elStatus.addEventListener('click', () => {
2741
return socket.close();
2842
});
29-
$('#send').click(function() {
30-
var payload;
31-
payload = new Object();
32-
payload.action = $('#action').val();
33-
payload.data = $('#data').val();
43+
44+
elSend.addEventListener('click', () => {
45+
let payload = {
46+
action: elAction.value,
47+
data: elData.value
48+
};
3449
return socket.send(JSON.stringify(payload));
3550
});
36-
return $('#sendfile').click(function() {
37-
var data, payload;
38-
data = document.binaryFrame.file.files[0];
39-
if (data) {
40-
payload = new Object();
41-
payload.action = 'setFilename';
42-
payload.data = $('#file').val();
43-
socket.send(JSON.stringify(payload));
44-
socket.send(data);
45-
}
46-
return false;
47-
});
51+
4852
});
4953

50-
}).call(this);
54+
}).call();

0 commit comments

Comments
 (0)