Skip to content

Commit

Permalink
let socket.io handle json
Browse files Browse the repository at this point in the history
  • Loading branch information
Visnu Pitiyanuvath committed Aug 9, 2011
1 parent 3e5cb2e commit 270bcd6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
8 changes: 3 additions & 5 deletions controllers/websocket.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ app = require '../config/app'

app.ws?.sockets.on 'connection', (client) ->
client.on 'message', (data) ->
return if client.lastMessageAt && Date.now() - client.lastMessageAt < 200
#console.log client.id, data.length, Date.now() - (client.lastMessageAt || 0)
return if Date.now() - client.lastMessageAt < 200
client.lastMessageAt = Date.now()
data = JSON.parse(data)
data.id = client.id
client.broadcast.send JSON.stringify(data)
client.json.broadcast.send data
client.on 'disconnect', ->
client.broadcast.send JSON.stringify(id: client.id, disconnect: true)
client.json.broadcast.send id: client.id, disconnect: true
5 changes: 2 additions & 3 deletions public/javascripts/watchmaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ var nko = {};
})();
});
ws.on('message', function(data) {
var data = JSON.parse(data)
, dude = dudes[data.id];
var dude = dudes[data.id];

if (data.disconnect && dude) {
dude.remove();
Expand Down Expand Up @@ -289,7 +288,7 @@ var nko = {};
nko.send = function send(data) {
if (!ws) return;
if (Date.now() - ws.lastSentAt < 10) throw Error('too fast'); // throttle
ws.send(JSON.stringify(data));
ws.json.send(data);
ws.lastSentAt = Date.now();

// TODO disconnect after 10 minutes of no real sending
Expand Down

0 comments on commit 270bcd6

Please sign in to comment.