Created
July 17, 2012 17:10
-
-
Save nmorse/3130632 to your computer and use it in GitHub Desktop.
basic nowJS on connect and disconnect methods
This file contains hidden or 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 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