Skip to content

Instantly share code, notes, and snippets.

@ideomix
Forked from dakatsuka/chat.js
Last active December 14, 2015 09:19
Show Gist options
  • Save ideomix/5064246 to your computer and use it in GitHub Desktop.
Save ideomix/5064246 to your computer and use it in GitHub Desktop.
// 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'));
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