Skip to content

Instantly share code, notes, and snippets.

@dakatsuka
Created June 19, 2011 11:27
Show Gist options
  • Save dakatsuka/1034095 to your computer and use it in GitHub Desktop.
Save dakatsuka/1034095 to your computer and use it in GitHub Desktop.
中継サーバに Redis Pub/Sub を使う
// 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'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment