-
-
Save anthonybrown/2701970 to your computer and use it in GitHub Desktop.
Socket.IO RedisStore and Rooms
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
//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"}); | |
}); | |
}); | |
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
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment