-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws_client_ts.ts
More file actions
23 lines (23 loc) · 888 Bytes
/
ws_client_ts.ts
File metadata and controls
23 lines (23 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var socket = new WebSocket("ws://localhost:1234");
socket.onopen = function (e) {
console.log("[open] Connection established");
console.log("Sending to server");
socket.send("My name is danx0r");
};
socket.onmessage = function (event) {
console.log("[message] Data received from server: ".concat(event.data));
document.getElementById("message").innerHTML = event.data;
};
socket.onclose = function (event) {
if (event.wasClean) {
console.log("[close] Connection closed cleanly, code=".concat(event.code as unknown as string, " reason=").concat(event.reason));
}
else {
// e.g. server process killed or network down
// event.code is usually 1006 in this case
console.log('[close] Connection died');
}
};
socket.onerror = function (error) {
console.log("[error] ".concat(error.currentTarget as unknown as string));
};