Skip to content

Instantly share code, notes, and snippets.

@nmorse
Created July 17, 2012 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nmorse/3130632 to your computer and use it in GitHub Desktop.
Save nmorse/3130632 to your computer and use it in GitHub Desktop.
basic nowJS on connect and disconnect methods
var node_static = require('node-static');
var node_static_file = new(node_static.Server)('./client');
var http = require('http'),
players = {},
server = http.createServer(function(req, res) {
// static file server
req.addListener('end', function () {
node_static_file.serve(req, res);
});
}).listen(80);
var nowjs = require("now");
var everyone = nowjs.initialize(server);
nowjs.on('connect', function() {
// new player has arrived!
var cid = this.user.clientId;
players[cid] = {};
players[cid].session = {"info":"whatever"};
});
nowjs.on('disconnect', function() {
// old player is leaving!
var cid = this.user.clientId;
console.log('disconnecting from '+cid);
delete players[cid];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment