Last active
March 29, 2021 21:12
-
-
Save bricecarpentier/5599799 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 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
Can you Please Help me to integrate your code with my API Server?