Skip to content

Instantly share code, notes, and snippets.

@angelo
Created May 19, 2010 02:03
Show Gist options
  • Save angelo/405861 to your computer and use it in GitHub Desktop.
Save angelo/405861 to your computer and use it in GitHub Desktop.
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