Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated to work with socket.io v0.7+

  • Loading branch information...
commit e3039bcc002a4cb655bd6f2d8940b533b018fab5 1 parent 2f5b0a9
@scottbw authored
View
5,201 client/socket.io.js
3,411 additions, 1,790 deletions not shown
View
21 client/wave.js
@@ -122,21 +122,18 @@
}
// Setup websockets
- io.setPath('socketio/');
- this.socket = new io.Socket("localhost", {"port":"8081"});
- this.socket.connect();
+ this.socket = io.connect("http://localhost:8081");
- //Setup events and callbacks
- this.socket.addEvent('message', function(data){
- var json = JSON.parse(data);
+ // Setup events and callbacks
+ this.socket.on('message', function(data){
// There are two types of data we can get - participants and state
- if (json.type == 'participants'){
- wave.__setParticipants(json.data);
+ if (data.type == 'participants'){
+ wave.__setParticipants(data.data);
if (wave.participant_callback) wave.participant_callback();
}
- if (json.type == 'state'){
+ if (data.type == 'state'){
// Apply delta to current state
- wave.getState().__applyPatches(json.data);
+ wave.getState().__applyPatches(data.data);
//wave.setState(json.data);
if (wave.state_callback) wave.state_callback();
}
@@ -192,7 +189,7 @@
if (window.Widget) msg.idkey = Widget.instanceid_key;
msg.sharedDataKey = wave.sharedDataKey;
msg.viewer = this.viewer;
- this.socket.send(JSON.stringify(msg));
+ this.socket.json.send(msg);
}
// Update the internal participants array
@@ -234,7 +231,7 @@
msg.key = "0";
if (window.widget) msg.key = Widget.instanceid_key;
msg.delta = thedelta;
- this.socket.send(JSON.stringify(msg));
+ this.socket.json.send(msg);
}
}
View
2  examples/server/server.js
@@ -26,7 +26,7 @@ var server = http.createServer(function(req, res){
});
var wave = require('wave').Server;
-wave.attach(server, {"host":"127.0.0.1", "port":"6379", "clear":false});
+wave.attach(server, {"host":"127.0.0.1", "port":"6379", "clear":true});
server.listen(PORT, "127.0.0.1");
console.log('Server running at http://127.0.0.1:'+PORT);
View
71 lib/wave.js
@@ -39,8 +39,10 @@ Server.prototype.attach = function(server, options){
// Use Redis as the default keystore - you can use any supported
// stores instead by editing this code.
this.store = new keys.Redis(options);
- if (options.clear) this.store.clear(); // Clear the store
-
+ if (options.clear){
+ this.store.clear(); // Clear the store
+ console.log("info: cleared store");
+ }
/*
* Socket configuration
*
@@ -55,19 +57,17 @@ Server.prototype.attach = function(server, options){
// The array of currently-connected websocket clients
// socket.io
var that = this;
- var io = require('socket.io'); // for npm, otherwise use require('./path/to/socket.io')
- var socket = io.listen(server);
- socket.on('connection', function(client){
+ var io = require('socket.io').listen(server); // for npm, otherwise use require('./path/to/socket.io')
+ io.sockets.on('connection', function(client){
// Got a message
client.on('message', function(message){
- var messageJSON = JSON.parse(message);
// register client
- if (messageJSON.sharedDataKey){
- that.registerClient(client, messageJSON.sharedDataKey);
- that.addParticipant(client, messageJSON.sharedDataKey, messageJSON.viewer.Participant);
+ if (message.sharedDataKey){
+ that.registerClient(client, message.sharedDataKey);
+ that.addParticipant(client, message.sharedDataKey, message.viewer.Participant);
} else {
// delta
- that.submitDelta(client, messageJSON.delta);
+ that.submitDelta(client, message.delta);
}
});
// De-register the client on disconnect
@@ -75,9 +75,9 @@ Server.prototype.attach = function(server, options){
// Remove the client from the array of currently connected clients
// This will stop us trying to send notifications to it
- var sessionId = client.sessionId;
+ var sessionId = client.id;
that.clients = that.clients.filter(function(client){
- if (client.sessionId == sessionId) return false;
+ if (client.id == sessionId) return false;
return true;
});
@@ -110,16 +110,16 @@ process.on('uncaughtException', function (err) {
Server.prototype.saveState = function(sharedDataKey, state){
this.store.set(sharedDataKey+"_state", JSON.stringify(state));
};
-// Create a JSON message to return state data to the client
-Server.prototype.stateToJSON = function(state){
+// Create an object in which to return state data to the client
+Server.prototype.createStateMessage = function(state){
var msg = {};
msg.type="state";
msg.data=state;
- return JSON.stringify(msg);
+ return msg;
};
// Process a Delta
Server.prototype.submitDelta = function(client, delta){
- var id = client.sessionId;
+ var id = client.id;
var state;
var that = this;
this.store.get(id, function(err, sharedDataKey){
@@ -147,9 +147,7 @@ Server.prototype.submitDelta = function(client, delta){
// Save state
that.saveState(sharedDataKey, state);
// Propagate changes to connected sibling clients
- that.notifySiblings(sharedDataKey, that.stateToJSON(delta));
- // Or just tell all connected clients...
- //socket.broadcast(stateToJSON(state));
+ that.notifySiblings(sharedDataKey, that.createStateMessage(delta));
});
});
};
@@ -158,12 +156,12 @@ Server.prototype.submitDelta = function(client, delta){
* Participant management
*/
-// Create a JSON message to return participants data to clients
-Server.prototype.participantsToJSON = function(participants){
+// Create an object to return participants data to clients
+Server.prototype.createParticipantsMessage = function(participants){
var msg = {};
msg.type="participants";
msg.data=participants;
- return JSON.stringify(msg);
+ return msg;
};
// Persist a participant
@@ -185,10 +183,10 @@ Server.prototype.addParticipant = function(client, sharedDataKey, participant){
if (!participants[participant.participant_id]) {
// Add session id for this participant so we can remove it
// when the client disconnects
- participant.sessionId = client.sessionId;
+ participant.sessionId = client.id;
participants[participant.participant_id]=participant;
that.saveParticipants(sharedDataKey, participants);
- that.notifySiblings(sharedDataKey, that.participantsToJSON(participants));
+ that.notifySiblings(sharedDataKey, that.createParticipantsMessage(participants));
}
});
};
@@ -211,7 +209,7 @@ Server.prototype.removeParticipant = function(sessionId){
if (participants[participant].sessionId == sessionId) {
delete participants[participant];
that.saveParticipants(sharedDataKey, participants);
- that.notifySiblings(sharedDataKey, that.participantsToJSON(participants));
+ that.notifySiblings(sharedDataKey, that.createParticipantsMessage(participants));
}
}
@@ -234,19 +232,24 @@ Server.prototype.registerClient = function(client, sharedDataKey){
this.store.get(sharedDataKey+"_state", function(err, state){
if (state){
var patch = that.createInitialPatch(state);
- client.send(that.stateToJSON(patch));
+ client.json.send(that.createStateMessage(patch));
} else {
- client.send(that.stateToJSON({}));
+ client.json.send(that.createStateMessage({}));
}
});
- // Send initial participant data to the client
- this.store.get(sharedDataKey+"_participants", function(err, participants){
- if (participants) client.send(that.participantsToJSON(JSON.parse(participants)));
- });
+ //
+ // This isn't necessary, in fact it causes problems with concurrency
+ // as a "blank" participant set can be sent out and overtake the result
+ // of the client registering itself.
+ //
+ //Send initial participant data to the client
+ //this.store.get(sharedDataKey+"_participants", function(err, participants){
+ //if (participants) client.json.send(that.createParticipantsMessage(JSON.parse(participants)));
+ //});
// Save the client details
- this.store.set(client.sessionId, sharedDataKey);
+ this.store.set(client.id, sharedDataKey);
};
@@ -272,8 +275,8 @@ Server.prototype.notifySiblings = function(sharedDataKey, message){
}
};
Server.prototype.notify = function(target, sharedDataKey, message){
- this.store.get(target.sessionId, function(err, key){
+ this.store.get(target.id, function(err, key){
key = key.toString();
- if (key == sharedDataKey) target.send(message);
+ if (key == sharedDataKey) target.json.send(message);
});
};
View
6 package.json
@@ -3,8 +3,8 @@
, "keywords" : ["wave", "websocket", "google wave"]
, "author" : "Scott Wilson <scott.bradley.wilson@gmail.com>"
-, "version" : "0.2.1"
-, "engines" : {"node": ">=0.2.4"}
+, "version" : "0.2.2"
+, "engines" : {"node": ">=0.4.0"}
, "bugs" : "http://github.com/scottbw/wave-node/issues"
@@ -27,7 +27,7 @@
]
, "dependencies" : { "keys" : ">=0.1.2"
- ,"socket.io": ">=0.6.8"
+ ,"socket.io": ">=0.7.0"
,"redis": ">=0.5.2"
}
Please sign in to comment.
Something went wrong with that request. Please try again.