-
-
Save ideomix/5064246 to your computer and use it in GitHub Desktop.
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 program is free software. It comes without any warranty, to | |
// the extent permitted by applicable law. You can redistribute it | |
// and/or modify it under the terms of the Do What The Fuck You Want | |
// To Public License, Version 2, as published by Sam Hocevar. See | |
// http://sam.zoy.org/wtfpl/COPYING for more details. | |
var sys = require('sys') | |
, opts = require('opts') | |
, ws = require('websocket-server') | |
, redis = require('redis') | |
, server = ws.createServer() | |
, subscriber = redis.createClient(6379, 'localhost') | |
, publisher = redis.createClient(6379, 'localhost'); | |
opts.parse([ | |
{ | |
'short': 'p', | |
'long': 'port', | |
'description': 'WebSocket Port', | |
'value': true, | |
'required': true | |
} | |
]); | |
subscriber.on("error", function(err) { | |
sys.debug(err); | |
}); | |
publisher.on("error", function(err) { | |
sys.debug(err); | |
}); | |
subscriber.subscribe("chat"); | |
subscriber.on("message", function(channel, message) { | |
sys.puts(message); | |
server.broadcast(message); | |
}); | |
server.addListener("connection", function(connection) { | |
sys.puts("client connected: " + connection.id); | |
connection.addListener("message", function(message) { | |
publisher.publish("chat", message); | |
}); | |
}); | |
server.addListener("close", function(connection) { | |
sys.puts("client disconnected: " + connection.id); | |
}); | |
server.listen(opts.get('port')); |
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
npm install opts | |
npm install websocket-server | |
npm install redis | |
npm install hiredis | |
を最初にやっておかないと起動できません。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment