Created
June 18, 2013 22:53
-
-
Save scottweinert/5810239 to your computer and use it in GitHub Desktop.
Nodejitsu with socket.io - not working
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
/** | |
* Start NodeJS Server | |
*/ | |
var port = 80; | |
if (process.env.CRON) { port = 3000; } | |
var secureServer = require('https').createServer(options, app) | |
// Initialize Socket IO | |
Common.io = require('socket.io').listen(secureServer) | |
// Only start SSL on non-production - NodeJitsu does this for us | |
if (process.env.NODE_ENV === 'development' || process.env.CRON) { | |
secureServer.listen(443, function(){ | |
console.log("Express server listening on port " + port) | |
}); | |
} | |
// Start server | |
app.listen(port, function(){ | |
console.log("Pingplot Server Started"); | |
}); | |
// Use RedisStore for socket IO connections | |
var redisClient = redis.createClient(Common.conf.redis_config.port, Common.conf.redis_config.host) | |
, redisPub = redis.createClient(Common.conf.redis_config.port, Common.conf.redis_config.host) | |
, redisSub = redis.createClient(Common.conf.redis_config.port, Common.conf.redis_config.host) | |
redisClient.auth(Common.conf.redis_config.pass, function (err) { if (err) throw err; }); | |
redisPub.auth(Common.conf.redis_config.pass, function (err) { if (err) throw err; }); | |
redisSub.auth(Common.conf.redis_config.pass, function (err) { if (err) throw err; }); | |
Common.io.set('store', new IoRedisStore({ | |
redis: redis | |
, redisPub : redisPub | |
, redisSub : redisSub | |
, redisClient : redisClient | |
})); | |
// Configure Socket IO | |
Common.io.enable('browser client minification'); // send minified client | |
Common.io.enable('browser client etag'); // apply etag caching logic based on version number | |
Common.io.enable('browser client gzip'); // gzip the file | |
Common.io.set('log level', 1); // reduce logging | |
// enable all transports (optional if you want flashsocket support, please note that some hosting | |
// providers do not allow you to create servers that listen on a port different than 80 or their | |
// default port) | |
Common.io.set('transports', [ | |
'websocket' | |
// , 'flashsocket' // Breaks NodeJitsu with Parse Error | |
, 'htmlfile' | |
, 'xhr-polling' | |
, 'jsonp-polling' | |
]); | |
Common.io.sockets.on('connection', function (socket) { | |
socket.emit('register'); | |
socket.on('setUser', function (data) { | |
Model.User.update({_id: data.user}, { socket: socket.id }, null, null); | |
}); | |
socket.on('disconnect', function() { | |
Model.User.update({socket: socket.id}, { socket: '' }, null, null); | |
// delete clients[socket.id]; // delete the client from the list | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment