Created
August 22, 2014 14:12
-
-
Save Ugmaxie/e062ea4ea51ecf1a6c80 to your computer and use it in GitHub Desktop.
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
/** | |
* Created by maxie on 8/22/14. | |
*/ | |
var app = require('http').createServer(handler); | |
var port = process.env.PORT; | |
app.listen(port); | |
var io = require('socket.io').listen(app); | |
var redis = require('redis'); | |
var fs = require('fs'); | |
function handler(req,res){ | |
fs.readFile(__dirname + '/index.html', function(err,data){ | |
if(err){ | |
res.writeHead(500); | |
return res.end('Error loading index.html'); | |
} | |
res.writeHead(200); | |
console.log("Listening on port " + port); | |
res.end(data); | |
}); | |
} | |
var store = redis.createClient(); | |
var pub = redis.createClient(); | |
var sub = redis.createClient(); | |
io.sockets.on('connection', function (client) { | |
sub.subscribe("chatting"); | |
sub.on("message", function (channel, message) { | |
console.log("message received on server from publish "); | |
client.send(message); | |
}); | |
client.on("message", function (msg) { | |
console.log(msg); | |
if(msg.type == "chat"){ | |
pub.publish("chatting",msg.message); | |
} | |
else if(msg.type == "setUsername"){ | |
pub.publish("chatting","A new user is connected: " + msg.user); | |
store.sadd("onlineUsers",msg.user); | |
} | |
}); | |
client.on('disconnect', function () { | |
sub.quit(); | |
pub.publish("chatting","User is disconnected :" + client.id); | |
}); | |
}); |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Socket and Redis in Node.js</title> | |
<script src="/socket.io/socket.io.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> | |
<style> | |
.username{ | |
display: inline-block; | |
background-color: #ccc; | |
border-radius: 5px; | |
padding: 2px 6px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="username"> | |
<input type="text" name="usernameTxt" /> <input type="button" name="setUsername" value="Set Username" /> | |
</div> | |
<div id="sendChat" style="display:none;"> | |
<input type="text" name="chatTxt" /> <input type="button" name="sendBtn" value="Send" /> | |
</div> | |
<br /> | |
<div id="content"></div> | |
<script> | |
$(document).ready(function() { | |
var username = "anonimus"; | |
$('input[name=setUsername]').click(function(){ | |
if($('input[name=usernameTxt]').val() != ""){ | |
username = $('input[name=usernameTxt]').val(); | |
var msg = {type:'setUsername',user:username}; | |
socket.json.send(msg); | |
} | |
$('#username').before("<div class='username'>" + username + "</div>"); | |
$('#username').slideUp("slow",function(){ | |
$('#sendChat').slideDown("slow"); | |
}); | |
}); | |
var socket = new io.connect('http://localhost'); | |
var content = $('#content'); | |
socket.on('connect', function() { | |
console.log("Connected"); | |
}); | |
socket.on('message', function(message){ | |
content.append(message + '<br />'); | |
}) ; | |
socket.on('disconnect', function() { | |
console.log('disconnected'); | |
content.html("<b>Disconnected!</b>"); | |
}); | |
$("input[name=sendBtn]").click(function(){ | |
var msg = {type:'chat',message:username + " : " + $("input[name=chatTxt]").val()} | |
socket.json.send(msg); | |
$("input[name=chatTxt]").val(""); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment