-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsockets.js
67 lines (57 loc) · 1.79 KB
/
websockets.js
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
$(document).ready(function(){
var connection;
var canvas;
var ctx;
function initConnection(){
connection = new WebSocket('ws://ws.pierre-paul.com:9001/echo');
logMessage('creating socket');
connection.onopen = function () {
logError('Connection open');
};
connection.onerror = function (error) {
logError(error + '<br />');
};
connection.onmessage = function (e) {
logMessage('< ' + e.data);
infos = jQuery.parseJSON(e.data);
clearPixels(infos.x, infos.y);
};
connection.onclose = function (e) {
if(e.wasClean == true){
logError('Closing connection');
}else{
logError('Unable to connect');
}
}
}
initConnection();
initCanvas();
$("button").click(function(){
console.log(connection);
if(connection.readyState == connection.OPEN){
connection.send($("#input-message").val());
//logMessage('> ' + $("#input-message").val())
}else if(connection.readyState !== connection.CONNECTING){
initConnection();
//$(this).trigger('click');
}
});
function logMessage(message){
console.log(message);
$("#messages").append(message + '<br />');
}
function logError(error){
//$("#websocket-infos").append(error+ '<br />');
console.log(error);
}
function initCanvas(){
canvas = $("#websocket-infos").get(0);
canvas.setAttribute('height', 280);
canvas.setAttribute('width', 220);
ctx = canvas.getContext('2d');
ctx.fillRect(0,0,220,283);
}
function clearPixels(x,y){
ctx.clearRect(x*10, y*10, 10,10);
}
});