Created
May 19, 2010 02:03
-
-
Save angelo/405861 to your computer and use it in GitHub Desktop.
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
var System = require('sys'); | |
var WebSocket = require('../lib/websocket').WebSocket; | |
var Connection = require('../lib/connection').Connection; | |
var Connections = {}; | |
var | |
Client = Connection; | |
Client.prototype.onmessage = function(data) { | |
var data = JSON.parse(data); | |
switch (data.type) { | |
case 'join': | |
this.name = data.name; | |
Connections[data.name] = this; | |
for (var name in Connections) { | |
var | |
connection = Connections[name]; | |
connection.send(JSON.stringify({ | |
'name' : this.name, | |
'type' : 'join' | |
})); | |
if (this.name != name) { | |
this.send(JSON.stringify({ | |
'name' : name, | |
'type' : 'join', | |
'x' : connection.x, | |
'y' : connection.y | |
})); | |
} | |
}; | |
break; | |
case 'leave': | |
for (var name in Connections) { | |
}; | |
break; | |
case 'move': | |
this.x = data.x || 32; | |
this.y = data.y || 32; | |
for (var name in Connections) { | |
var | |
connection = Connections[name]; | |
connection.send(JSON.stringify({ | |
'x' : data.x, | |
'y' : data.y, | |
'name' : this.name, | |
'type' : 'move' | |
})); | |
}; | |
break; | |
case 'speak': | |
for (var name in Connections) { | |
var | |
connection = Connections[name]; | |
connection.send(JSON.stringify({ | |
'data' : data.data, | |
'name' : this.name, | |
'type' : 'speak' | |
})); | |
}; | |
break; | |
} | |
}; | |
Client.prototype.onclose = function() {}; | |
new WebSocket(Client); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment