public
Last active

Socket.IO RedisStore and Rooms

  • Download Gist
app.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
//app.js Socket IO Test
var app = require('express').createServer(),
redis = require('socket.io/node_modules/redis'),
io = require('socket.io').listen(app);
 
var pub = redis.createClient(port, "url");
var sub = redis.createClient(port, "url");
var store = redis.createClient(port, "url");
pub.auth('pass', function(){console.log("adentro! pub")});
sub.auth('pass', function(){console.log("adentro! sub")});
store.auth('pass', function(){console.log("adentro! store")});
 
io.configure( function(){
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
var RedisStore = require('socket.io/lib/stores/redis');
io.set('store', new RedisStore({redisPub:pub, redisSub:sub, redisClient:store}));
});
 
app.listen(8000);
 
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
 
var buffer = [];
io.sockets.on('connection', function(client){
var Room = "";
client.on("setNickAndRoom", function(nick, fn){
fn({msg : "Hello " + nick.nick});
client.join(nick.room);
Room = nick.room;
client.broadcast.to(Room).json.send({ msg: "Se conecto al room: " + nick.room, nick : nick });
});
 
client.on('message', function(message, fn){
var msg = message; //{ message: [client.sessionId, message] };
buffer.push(msg);
if (buffer.length > 15)
buffer.shift();
client.broadcast.to(Room).json.send(msg);
fn(msg);
});
 
client.on('disconnect', function(){
client.broadcast.to(Room).json.send({ msg: "Se desconecto"});
});
});
index.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Socket IO Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<h1>Socket IO Test</h1>
<div id="setNickSpace">
Nickname: <input type="text" id="nickname"/> <br/>
Room: <input type="text" id="room"/> <button id="setNickName">Set</button>
</div>
<div id="chat" style="display: none;">
<div id="board"> </div>
<textarea id="textSend"></textarea>
<button id="send">Send</button>
</div>
</body>
<script type="text/javascript">
var chati = null;
$(document).ready(function(){
$("#setNickName").click(function(){
$("#setNickSpace").hide();
$("#chat").show();
chati = new Chat;
chati.Connect($("#nickname").val(), $("#room").val());
} );
$('textarea#textSend').bind('keypress', function(e) {
if(e.keyCode==13){
sendMsg();
}
});
$("#send").click(function()
{
sendMsg();
});
function sendMsg(){
var m = $("#textSend").val();
$("#textSend").val("");
chati.Send(m);
}
var today = new Date();
var offset = -(today.getTimezoneOffset()/60);
});
function Chat(){
this.socket = null;
this.Nickname = "";
this.Room = "";
this.Connect = function(nick, room){
socket = io.connect('http://192.168.55.100:8000');
Nickname = nick;
Room = room;
//conectarse
socket.on('connect',function (data) {
socket.emit('setNickAndRoom', {nick: nick, room: room}, function(response){
$("#board").append("<p>" + response.msg + "</p>");
});
});
//mensajes
socket.on("message", function(msg, p, c){
$("#board").append("<p>" + msg.nick + ": " + msg.msg + "</p>");
});
};
this.Send = function Send (msg) {
socket.emit("message", {msg: msg, nick: Nickname} , function(response) {
$("#board").append("<p>" + Nickname + ": " + msg + "</p>");
});
};
}
</script>
</html>

Very goood (Cojonudo, gracias... ;-) )

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.