2014-02-10 07:03:06 +00:00
|
|
|
/*var http = require('http'),
|
|
|
|
express = require('express'),
|
|
|
|
port = process.env.PORT || 5001,
|
|
|
|
app = express(),
|
|
|
|
server = http.createServer(app).listen(port),
|
|
|
|
io = require('socket.io').listen(server);
|
|
|
|
|
|
|
|
console.log(port);
|
|
|
|
|
|
|
|
app.configure(function() {
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
res.header('Access-Control-Allow-Origin', '*');
|
|
|
|
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
|
|
|
|
res.header('Access-Control-Allow-Headers', 'Content-Type');
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
}); */
|
|
|
|
|
|
|
|
//var rtg = require("url").parse("redis://redistogo:0ca046a3d56533cc162e2447db383192@pearlfish.redistogo.com:9060/"),
|
2014-06-04 20:24:13 +00:00
|
|
|
//redis = require('redis').createClient(rtg.port, rtg.hostname, {no_ready_check: true});
|
2013-04-26 04:07:29 +00:00
|
|
|
|
2014-02-10 07:03:06 +00:00
|
|
|
var io = require('socket.io').listen(5001);
|
|
|
|
var redis = require('redis').createClient();
|
2014-06-04 20:24:13 +00:00
|
|
|
|
2014-02-10 07:03:06 +00:00
|
|
|
//redis.auth(rtg.auth.split(":")[1], function() {
|
|
|
|
// start();
|
|
|
|
//});
|
|
|
|
|
|
|
|
function start() {
|
2014-06-04 20:24:13 +00:00
|
|
|
redis.subscribe('maps');
|
2013-04-26 04:07:29 +00:00
|
|
|
|
2014-06-04 20:24:13 +00:00
|
|
|
io.on('connection', function (socket) {
|
|
|
|
|
|
|
|
// this will ping everyone on a map with updates to the map
|
|
|
|
redis.on('message', function (channel, message) {
|
|
|
|
console.log(message);
|
|
|
|
var m = JSON.parse(message);
|
|
|
|
var room = 'maps-' + m.mapid;
|
|
|
|
|
|
|
|
socket.emit(room, m);
|
2014-06-04 19:24:16 +00:00
|
|
|
});
|
2014-06-04 20:24:13 +00:00
|
|
|
|
|
|
|
// this will ping a new person with awareness of who's already on the map
|
|
|
|
socket.on('updateNewMapperList', function (data) {
|
|
|
|
var existingUser = {
|
|
|
|
userid: data.userid,
|
|
|
|
username: data.username
|
|
|
|
};
|
|
|
|
socket.broadcast.emit(data.userToNotify + '-' + data.mapid + '-UpdateMapperList', existingUser);
|
2014-06-04 19:24:16 +00:00
|
|
|
});
|
2014-06-04 20:24:13 +00:00
|
|
|
|
|
|
|
// this will ping everyone on a map that there's a person just joined the map
|
|
|
|
socket.on('newMapperNotify', function (data) {
|
|
|
|
socket.set('mapid', data.mapid);
|
|
|
|
socket.set('userid', data.userid);
|
|
|
|
socket.set('username', data.username);
|
|
|
|
|
|
|
|
var newUser = {
|
|
|
|
userid: data.userid,
|
|
|
|
username: data.username
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.broadcast.emit('maps-' + data.mapid + '-newmapper', newUser);
|
2014-06-04 19:24:16 +00:00
|
|
|
});
|
2014-06-04 20:24:13 +00:00
|
|
|
|
|
|
|
// this will ping everyone on a map that there's a person just left the map
|
|
|
|
socket.on('disconnect', function () {
|
|
|
|
var socketUserName, socketUserID;
|
|
|
|
socket.get('userid', function (err, id) {
|
|
|
|
socketUserID = id;
|
|
|
|
});
|
|
|
|
socket.get('username', function (err, name) {
|
|
|
|
socketUserName = name;
|
|
|
|
});
|
|
|
|
var data = {
|
|
|
|
username: socketUserName,
|
|
|
|
userid: socketUserID
|
|
|
|
};
|
|
|
|
socket.get('mapid', function (err, mapid) {
|
|
|
|
socket.broadcast.emit('maps-' + mapid + '-lostmapper', data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-06-04 19:24:16 +00:00
|
|
|
});
|
2014-02-10 07:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start();
|