-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
37 lines (32 loc) · 794 Bytes
/
Copy pathsocket.js
File metadata and controls
37 lines (32 loc) · 794 Bytes
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
// Create a new socket and connect
var socket = new WebSocket("ws://localhost:8080/");
socket.addEventListener("open", (event) => {
console.log(event);
});
socket.onopen = function(event) {
console.log(event);
}
// Receive data
socket.onmessage = function(event)
{
var json = JSON.parse(event.data);
console.log(json.message);
document.getElementById("chat").innerHTML += json.message + "<br>";
}
// Send data
function sendCommand(command)
{
var json = {
"command" : command
};
socket.send(JSON.stringify(json)+"\r\n");
}
// Quit by sending a command
function quit()
{
sendCommand("quit");
}
function send() {
var msg = document.getElementById("message").value;
socket.send(JSON.stringify({"command": "message", "message": msg})+"\r\n");
}