Created
October 14, 2014 21:06
-
-
Save DarkSeraphim/1c6d5e8c30510f1d7085 to your computer and use it in GitHub Desktop.
organized socket.io
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 express = require('express'); | |
| // ... | |
| var app = express(); | |
| // ... | |
| var server = app.listen(80, function() | |
| { | |
| console.log("Listening on port ", (server.address().port)); | |
| }); | |
| // Early binding: | |
| var io = require('/socket.io').bind(server); | |
| // Or late binding: | |
| var io = require('/socket.io').init(server); | |
| // << Do stuff with io >> | |
| io.bind(); | |
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 events = []; | |
| function register(event) | |
| { | |
| events.push(require("/"+event)); | |
| } | |
| register('login'); | |
| var io; | |
| var init = function(server) | |
| { | |
| if(!server) | |
| throw new Error("server cannot be null."); | |
| return (io = require('socket.io')(server)); | |
| }; | |
| module.exports.init = init; | |
| module.exports.bind = function(server) | |
| { | |
| if(!io) | |
| init(server); | |
| io.on('connection', function(socket) | |
| { | |
| events.forEach(function(event) | |
| { | |
| event.listen(socket); | |
| }); | |
| }); | |
| } |
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
| moduke.exports.listen = function(socket) | |
| { | |
| socket.on('login', function(data) | |
| { | |
| // Removed for security reasons | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment