-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit befd274
Showing
5 changed files
with
674 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
var sys = require("sys"), | ||
ws = require("./ws"); | ||
|
||
const CONNECTED= 1, | ||
DISCONNECTED = -1; | ||
|
||
var operations= []; | ||
var connections= {}; | ||
var no_connections= 0; | ||
var disconnect_reason = 'Closed by client'; | ||
|
||
//Packet types: | ||
const DEFAULT_PACKET= 1; | ||
|
||
/** | ||
* Flushes all connection queues. | ||
* @return {undefined} Nothing | ||
*/ | ||
function flush_queues() { | ||
for (var id in connections) { | ||
var connection = connections[id]; | ||
|
||
if (connection.state != CONNECTED) { | ||
continue; | ||
} | ||
connection.flush_queue(); | ||
} | ||
} | ||
|
||
setInterval(flush_queues, 100); | ||
|
||
|
||
ws.createServer(function (websocket) { | ||
var connection_id = 0, | ||
message_queue = [], | ||
options= { | ||
max_connections: 4 | ||
}; | ||
|
||
websocket.flush_queue= function() { | ||
var msg = null, | ||
packet_data = []; | ||
|
||
if( websocket.operationsSeen < operations.length ) { | ||
for( var i = websocket.operationsSeen; i < operations.length; i++ ) { | ||
var data = JSON.stringify( operations[i] ); | ||
packet_data.push(data); | ||
} | ||
|
||
websocket.operationsSeen= operations.length; | ||
var dataOnTheWire= '[' + DEFAULT_PACKET + ',[' + packet_data.join(',') + ']]'; | ||
this.write(dataOnTheWire); | ||
} | ||
} | ||
|
||
/** | ||
* Forces a connection to be disconnected. | ||
*/ | ||
websocket.kill = function(reason) { | ||
var disconnect_reason = reason || 'Unknown Reason'; | ||
this.close(); | ||
message_queue = []; | ||
} | ||
|
||
/** | ||
* Stringify speicified object and sends it to remote part. | ||
*/ | ||
websocket.post = function(data) { | ||
var packet = JSON.stringify(data); | ||
this.write(packet); | ||
} | ||
/** | ||
* Sets the state of the connection | ||
*/ | ||
websocket.set_state = function(new_state) { | ||
switch (new_state) { | ||
case CONNECTED: | ||
if (no_connections++ > options.max_connections) { | ||
websocket.kill('server busy'); | ||
return; | ||
} | ||
while (connections[++connection_id]); | ||
websocket.id = connection_id; | ||
websocket.operationsSeen=0; | ||
connections[websocket.id] = websocket; | ||
websocket.flush_queue(); | ||
break; | ||
case DISCONNECTED: | ||
if (websocket.id && connections[websocket.id]) { | ||
delete connections[websocket.id]; | ||
no_connections--; | ||
} | ||
break; | ||
} | ||
websocket.state = new_state; | ||
}; | ||
websocket.addListener('close', function() { | ||
websocket.set_state(DISCONNECTED); | ||
}).addListener("connect", function (resource) { | ||
// emitted after handshake | ||
sys.debug(sys.inspect(resource)) | ||
sys.debug("connect: " + resource); | ||
websocket.write("welcome\r\n"); | ||
websocket.set_state(CONNECTED); | ||
|
||
// setTimeout(websocket.close, 10 * 1000); // server closes connection after 10s, will also get "close" event | ||
|
||
}).addListener("data", function (data) { | ||
var packet = null; | ||
|
||
try { | ||
packet = JSON.parse(data); | ||
} catch(e) { | ||
sys.debug('Malformed message recieved'); | ||
sys.debug(sys.inspect(data)); | ||
websocket.kill('Malformed message sent by client'); | ||
return; | ||
} | ||
operations[operations.length] = packet[1]; | ||
}).addListener("close", function () { | ||
|
||
// emitted when server or client closes connection | ||
sys.debug("close"); | ||
|
||
}); | ||
}).listen(8080); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<html> | ||
<head> | ||
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> | ||
<script type="text/javascript" src="js/jquery.ws-0.3-noenc-pre.js"></script> | ||
<script type="text/javascript"> | ||
$(document).ready(function () { | ||
var inputchk= function(){ | ||
if($("#txt").val()===""){$("#txt").focus();return false} | ||
else if($("#txt").val()===""){$("#txt").focus();return false} | ||
else {return true} | ||
} | ||
var colour= "rgb(" + Math.floor((Math.random()*255))+"," + Math.floor((Math.random()*255)) + "," + Math.floor((Math.random()*255)) +")"; | ||
var canvas = document.getElementById("canvas"); | ||
var ctx = canvas.getContext("2d"); | ||
ctx.fillStyle = colour; | ||
ctx.fillRect (0, 0, 500, 10); | ||
|
||
var ws = $.ws.conn({ | ||
url : 'ws://localhost:8080', | ||
hbStr: null, | ||
renderbox: function(boxString) { | ||
var canvas = document.getElementById("canvas"); | ||
var ctx = canvas.getContext("2d"); | ||
ctx.fillStyle = boxString[5]; | ||
ctx.fillRect (boxString[1], boxString[2], boxString[3], boxString[4]); | ||
}, | ||
onopen : function () { | ||
console.log('connected'); | ||
var self= this; | ||
setInterval(function(){ | ||
|
||
var x= Math.random()*480, | ||
y= Math.random()*480, | ||
width= 55, | ||
height= 55; | ||
var box= ['box', x,y,width,height, colour]; | ||
//self.renderbox( box ); | ||
$(ws).wssend(JSON.stringify([1,box])); | ||
}, 1000) | ||
}, | ||
|
||
onmessage : function (event) { | ||
try { | ||
var packets=eval(event); | ||
for(var i=0;i< packets[1].length;i++) { | ||
console.log(JSON.stringify(packets[1][i])) | ||
// this.renderbox([packets[1][2],packets[1][3],packets[1][4],packets[1][5]]); | ||
this.renderbox(packets[1][i]); | ||
} | ||
} | ||
catch(e) { | ||
console.log(e +" : " + event); | ||
} | ||
}, | ||
|
||
onclose : function (event) { | ||
console.log('disconnected'); | ||
} | ||
}); | ||
}); | ||
</script> | ||
</head> | ||
<body> | ||
<canvas id="canvas" width="500" height="500"></canvas> | ||
</body> | ||
</html> |
Oops, something went wrong.