Permalink
Browse files

websockets POS and ERROR commands.

  • Loading branch information...
1 parent 7557072 commit d8db996bba2fbe0206fdf09ebea65562fc6feba4 @alkavan committed Apr 2, 2012
Showing with 39 additions and 8 deletions.
  1. +37 −5 js/script.js
  2. +2 −3 js/server.js
View
42 js/script.js
@@ -218,7 +218,7 @@ var mfg = function () {
this.shots = sjs.List();
this.initSocket();
- log(this._socket);
+
// try {
// this._socket.send('HELO');
@@ -245,6 +245,15 @@ var mfg = function () {
mfg.run = function (){
this.initTicker();
+
+ var player_pos = 'POS PLAYER '
+ +mfg.player_index+' '+mfg.player.sprite.x
+ +','+mfg.player.sprite.y;
+
+ setTimeout(function () {
+
+ mfg._socket.send(player_pos);
+ }, 1000);
};
/**
@@ -845,7 +854,6 @@ var mfg = function () {
};
}
- log(mfg.ent.factory(this, settings));
return mfg.ent.factory(this, settings);
};
@@ -958,12 +966,36 @@ var mfg = function () {
log('[WS] Socket Status: '+socket.readyState+' (open)');
socket.send('HELO');
- log('[WS] SENT: HELO');
+ log('[WS] -> HELO');
};
- socket.onmessage = function(msg){
- log('[WS] RECEIVED: ' + msg.data);
+ socket.onmessage = function(event){
+// log(event);
+
+ // decode command
+ var command = event.data.split(" ");
+
+ if(typeof command[0] == "undefined")
+ {
+
+ log('[WS] <- NO COMMAND GIVEN');
+ return;
+ }
+
+ switch(command[0])
+ {
+ case 'HELO':
+ log('[WS] <- '+event.data);
+ break;
+ case 'ERROR':
+ log('[WS] <- '+event.data);
+ break;
+ default:
+ log('[WS] '+'UNKNOWN COMMAND "'+command[0]+'"');
+ break;
+
+ }
};
socket.onclose = function(){
View
5 js/server.js
@@ -52,11 +52,10 @@ server.addListener("connection", function(conn){
// conn.broadcast("** "+conn.id+" connected");
conn.addListener("message", function(msg){
-
// decode command
var command = msg.split(" ");
- if(typeof command[0] == "undefined")
+ if(typeof command[0] === "undefined")
{
server.send(conn.id, "NO COMMAND GIVEN");
@@ -69,7 +68,7 @@ server.addListener("connection", function(conn){
server.send(conn.id, "HELO "+conn.id);
break;
default:
- server.send(conn.id, 'UNKNOWN COMMAND "'+command[0]+'"');
+ server.send(conn.id, "ERROR UNKNOWN COMMAND "+command[0]);
break;
}

0 comments on commit d8db996

Please sign in to comment.