Forked from bricecarpentier/websocket-nodejs-redis-server.js
Created
January 27, 2016 04:58
-
-
Save christopherdro/db792a0353e3626dea0d to your computer and use it in GitHub Desktop.
This is a simple websocket server coded using nodejs and http://einaros.github.io/ws/ connecting to a redis pubsub channel and sending messages to selected clients
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 ws = require('ws'), | |
nconf = require('nconf'), | |
redis = require('redis'); | |
nconf.argv() | |
.env(); | |
var server = new ws.Server({port: nconf.get('PORT')}); | |
var sockets = {}; | |
server.on('connection', function(socket) { | |
socket.on('message', function(message) { | |
var a = message.split('|'); | |
var playerId = a[0]; | |
console.log('received message from ' + playerId); | |
sockets[playerId] = socket; | |
}); | |
}); | |
var db = redis.createClient(nconf.get("REDIS_PORT"), nconf.get("REDIS_HOST")); | |
if (nconf.get('REDIS_PASSWORD')) { | |
db.auth(nconf.get("REDIS_PASSWORD")); | |
} | |
db.on('message', function(channel, message) { | |
if (channel == 'chanel') | |
{ | |
var a = message.split('>'); | |
var socket = sockets[a[0]]; | |
var m = a[1]; | |
// les messages sont supposés contenir les ID utilisateurs | |
//var socket = sockets[message]; | |
if (socket != undefined) | |
{ | |
socket.send(m); | |
} | |
} | |
}); | |
db.subscribe('channel'); | |
console.log('listening at ws://localhost:' + nconf.get('PORT')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment