-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsecho.html
46 lines (41 loc) · 1.06 KB
/
wsecho.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
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Websocket Echo</title>
<script language="javascript" type="text/javascript">
var wsUri = "ws://localhost:1234/ws";
var ws;
function wsConnect()
{
ws = new WebSocket( wsUri );
ws.onopen = function( e ) {
console.log( "CONNECTED" );
ws.send( "READY" );
};
ws.onclose = function( e ) {
console.log( "DISCONNECTED" );
};
ws.onmessage = function( e ) {
var data = e.data.split("\n");
for ( var line in data ) {
var msg = data[line].split(" ");
var cmd = msg[0];
if ( cmd == "PIXEL" ) {
var x = parseInt(msg[1]);
var y = parseInt(msg[2]);
var color = msg[3];
ctx.fillStyle = color;
ctx.fillRect( x, y, 1, 1 );
} else {
console.log( "Unknown command: " + cmd );
}
}
};
ws.onerror = function( e ) {
console.log( 'WS Error: ' + e.data );
};
}
window.addEventListener( "load", wsConnect, false );
</script>
<body>
<h2>Websocket Echo</h2>
</body>