-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
236 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
var sio = require('socket.io'), | ||
util = require('util'); | ||
|
||
/** | ||
* io module | ||
*/ | ||
var io = null; | ||
|
||
/** | ||
* Namespaces | ||
*/ | ||
var namespaces = {}; | ||
|
||
/** | ||
* Listen app and create Socket.IO server. | ||
* | ||
* @param {Object} app Express application | ||
*/ | ||
exports.listen = function(app) { | ||
if (!io) { | ||
io = sio.listen(app); | ||
|
||
io.configure('production', function() { | ||
io.enable('browser client minification'); | ||
io.enable('browser client etag'); | ||
io.enable('browser client gzip'); | ||
io.set('log level', 1); | ||
// Heroku is not support WebSocket | ||
io.set('transports', [ 'xhr-polling' ]); | ||
}); | ||
|
||
io.configure('development', function() { | ||
io.set('transports', ['websocket']); | ||
}); | ||
} | ||
}; | ||
|
||
/** | ||
* Get namespace for specified topic. | ||
* If namespace is not found, create new one and return it. | ||
* | ||
* @param {String} topicId Topic ID to create namespace | ||
* @return {Object} Socket.IO server for specified namespace. | ||
*/ | ||
exports.namespace = function(topicId) { | ||
if (namespaces[topicId]) { | ||
return namespaces[topicId]; | ||
} else { | ||
var namespace = | ||
io.of('/' + topicId) | ||
.on('connection', function(socket) { | ||
console.log('connected on ' + topicId); | ||
socket.on('error', function(e) { | ||
console.error(util.inspect(e, true)); | ||
}); | ||
}); | ||
namespaces[topicId] = namespace; | ||
return namespace; | ||
} | ||
}; |
This file contains 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
This file contains 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
This file contains 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