Last active
August 29, 2015 14:06
-
-
Save dux/a45824db24af313eaef4 to your computer and use it in GitHub Desktop.
NodeJS PubSub server - simple firebase API clone
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
// PubSub | |
window.PubSub = { | |
socket: null, | |
connect: function(url) { | |
$.get(url, function(response) { | |
console.log('SocketIO: '+response) | |
PubSub.socket = io.connect(url); | |
PubSub.socket.on('msg', PubSub.execute) | |
}); | |
}, | |
execute: function(data) { | |
if ( ! PubSub.socket ) return | |
PubSub[data._func](data) | |
}, | |
emit: function(func, data) { | |
if ( ! PubSub.socket ) return | |
data = data || {} | |
data['_func'] = func | |
PubSub.socket.emit('all', data); | |
}, | |
} | |
// site data | |
PubSub.mouse = function (data) { $('#mouse').val(data.pos) }, | |
PubSub.chat = function (data) { $('textarea')[0].value += data.nick+": "+data.data+"\n" } | |
PubSub.connect('http://localhost:8000/'+(location.href.split('?')[1]||'')) | |
function add_to_chat() { | |
nick = $('#nick').val() || 'guest' | |
data = $('#textdata').val(); | |
PubSub.emit('chat', { nick:nick, data:data }) | |
} | |
$(document).mousemove(function(e){ | |
PubSub.emit('mouse', { pos:e.pageX+'-'+e.pageY }) | |
}) | |
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
sudo npm install nodemon -g | |
nodemon server.coffee | |
open stand-alone.html | |
stand-alone.html?room1 | |
stand-alone.html?room2 | |
stand-alone.html?room3 | |
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
express = require("express") | |
app = express() | |
server = require("http").createServer(app) | |
io = require("../..")(server) | |
port = process.env.PORT or 8000 | |
server.listen port, -> | |
console.log "Server listening at port %d", port | |
return | |
ios = {} | |
# every path is protexted namespace | |
app.get '/*', (req, res) -> | |
ns_name = req.path | |
res.set 'Access-Control-Allow-Origin': '*' | |
if ios[ns_name] | |
res.send(200, "Using: #{ns_name}") | |
else | |
ios[ns_name] = io.of(ns_name).on "connection", (socket) -> | |
socket.on "all", (data) -> | |
console.log [ns_name, 'all', data] | |
io.of(ns_name).emit('msg', data); | |
res.send(200, "Created: #{ns_name}") | |
if Object.keys(req.query).length > 0 | |
console.log ["qs:#{ns_name}", 'all', req.query] | |
io.of(ns_name).emit('msg', req.query); | |
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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Socket.IO Chat Example</title> | |
<link rel="stylesheet" href="style.css"> | |
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script> | |
<script src="http://localhost:8000/socket.io/socket.io.js"></script> | |
<script src="./client.js"></script> | |
</head> | |
<body style="padding:30px;"> | |
<input id="nick" /> | |
<input id="textdata" /> | |
<button onclick="add_to_chat()">Test it</button> | |
<br /> | |
<textarea style="width:400px; height:200px;"></textarea> | |
<br /> | |
<br /> | |
<input id="mouse" /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment