summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreeyorp <TheFreeYorp@NOSPAM.G.m.a.i.l.replace>2013-05-15 07:05:54 +1200
committerFreeyorp <TheFreeYorp@NOSPAM.G.m.a.i.l.replace>2013-05-15 07:05:54 +1200
commit3810834670aa3fc73357a6540269d0e3eaf8141d (patch)
tree7758be12e0c1622935597476beb7c63ba5915a4e
parent57c588f84f50cd33dbbf0494bfef707e3928ba1a (diff)
downloadmanavis-3810834670aa3fc73357a6540269d0e3eaf8141d.tar.gz
manavis-3810834670aa3fc73357a6540269d0e3eaf8141d.tar.bz2
manavis-3810834670aa3fc73357a6540269d0e3eaf8141d.tar.xz
manavis-3810834670aa3fc73357a6540269d0e3eaf8141d.zip
Remove login; apply handlers on connection
This should hopefully make things apply more cleanly for reconnections and fallbacks.
-rw-r--r--index.js228
-rw-r--r--public/js/mv/connect.js3
2 files changed, 114 insertions, 117 deletions
diff --git a/index.js b/index.js
index 339422a..0276a68 100644
--- a/index.js
+++ b/index.js
@@ -54,130 +54,128 @@ sessionSockets.on('connection', function (err, socket, session) {
* Don't do anything until they send a login message.
* Later versions might also check a protocol version here.
*/
- socket.on('login', function() {
- /* Someone new connected. Restore or initialise their session data. */
- var logAction = entityLogger(session.nid);
- session.nid = session.nid || (++count);
- session.nick = session.nick || null;
- session.save();
- if (session.nid in sockets) {
- /* Ghost the old session */
- ghosting = true;
- sockets[session.nid].disconnect();
- ghosting = false;
+ /* Someone new connected. Restore or initialise their session data. */
+ var logAction = entityLogger(session.nid);
+ session.nid = session.nid || (++count);
+ session.nick = session.nick || null;
+ session.save();
+ if (session.nid in sockets) {
+ /* Ghost the old session */
+ ghosting = true;
+ sockets[session.nid].disconnect();
+ ghosting = false;
+ }
+ sockets[session.nid] = socket;
+ /* New user! */
+ logAction("CONNECT", { "ip": socket.handshake.address.address, "proxied-ip": socket.handshake.headers['x-forwarded-for'] });
+ users[session.nid] = { nick: session.nick, filters: {} };
+ /* Let them know of their data. */
+ socket.emit('selflogin', {
+ id: session.nid,
+ nick: session.nick
+ });
+ /* Let everyone else know that someone connected. */
+ socket.broadcast.emit('login', {
+ id: session.nid,
+ nick: session.nick
+ });
+ /* Send the new user the userlist. */
+ socket.emit('users', { users: users });
+ /* Set up various handlers for the new socket. */
+ socket.on('nick', function (d) {
+ if (!(typeof(d) == "object" && "nick" in d)) {
+ return;
}
- sockets[session.nid] = socket;
- /* New user! */
- logAction("CONNECT", { "ip": socket.handshake.address.address, "proxied-ip": socket.handshake.headers['x-forwarded-for'] });
- users[session.nid] = { nick: session.nick, filters: {} };
- /* Let them know of their data. */
- socket.emit('selflogin', {
- id: session.nid,
- nick: session.nick
- });
- /* Let everyone else know that someone connected. */
- socket.broadcast.emit('login', {
+ /* TODO Collision checking? */
+ users[session.nid].nick = session.nick = d.nick;
+ session.save();
+ logAction("NICK", d.nick);
+ io.sockets.emit('nickset', {
id: session.nid,
- nick: session.nick
- });
- /* Send the new user the userlist. */
- socket.emit('users', { users: users });
- /* Set up various handlers for the new socket. */
- socket.on('nick', function (d) {
- if (!(typeof(d) == "object" && "nick" in d)) {
- return;
- }
- /* TODO Collision checking? */
- users[session.nid].nick = session.nick = d.nick;
- session.save();
- logAction("NICK", d.nick);
- io.sockets.emit('nickset', {
- id: session.nid,
- nick: d.nick
- });
- });
- socket.on('join', function(d) {
- var channel;
- if (d != null) {
- if (!(typeof(d) == "number" && d <= channelCount)) {
- return;
- }
- /* Join an existing channel */
- channel = d;
- } else {
- /* Automagically create a new channel */
- channel = ++channelCount;
- }
- logAction("JOIN", users[session.nid].channel);
- if ("channel" in users[session.nid]) {
- /* Leave any channel we're in */
- socket.leave(users[session.nid].channel);
- }
- /* Inform socket.io about the channel join */
- socket.join(channel);
- /* Let everyone know about the channel join */
- users[session.nid].channel = channel;
- io.sockets.emit('join', {
- id: session.nid,
- channel: channel
- });
- /* Update the channel information */
- if (channel in channels) {
- /* This channel already exists. Inform the joining user of the current filters. */
- socket.emit('filterset', {
- id: 0, /* Server */
- filters: channels[channel].filters
- });
- ++channels[channel].usernum;
- } else {
- /* This channel didn't already exist, so create it and set the filters. */
- channels[channel] = {
- usernum: 1,
- filters: users[session.nid].filters
- };
- }
+ nick: d.nick
});
- socket.on('part', function() {
- if (!users[session.nid].channel) {
+ });
+ socket.on('join', function(d) {
+ var channel;
+ if (d != null) {
+ if (!(typeof(d) == "number" && d <= channelCount)) {
return;
}
- logAction("PART");
+ /* Join an existing channel */
+ channel = d;
+ } else {
+ /* Automagically create a new channel */
+ channel = ++channelCount;
+ }
+ logAction("JOIN", users[session.nid].channel);
+ if ("channel" in users[session.nid]) {
+ /* Leave any channel we're in */
socket.leave(users[session.nid].channel);
- if (!--channels[users[session.nid].channel].usernum) {
- delete channels[users[session.nid].channel];
- }
- delete users[session.nid].channel;
- io.sockets.emit('part', {
- id: session.nid
- });
+ }
+ /* Inform socket.io about the channel join */
+ socket.join(channel);
+ /* Let everyone know about the channel join */
+ users[session.nid].channel = channel;
+ io.sockets.emit('join', {
+ id: session.nid,
+ channel: channel
});
- socket.on('filter', function(d) {
- if (!(typeof(d) == "object" && "filters" in d)) {
- return;
- }
- users[session.nid].filters = d.filters;
- logAction("FILTER", d.filters);
- var channel = users[session.nid].channel;
- if (!channel) {
- return;
- }
- channels[channel].filters = d.filters;
- socket.broadcast.to(channel).emit('filterset', {
- id: session.nid,
- filters: d.filters
+ /* Update the channel information */
+ if (channel in channels) {
+ /* This channel already exists. Inform the joining user of the current filters. */
+ socket.emit('filterset', {
+ id: 0, /* Server */
+ filters: channels[channel].filters
});
+ ++channels[channel].usernum;
+ } else {
+ /* This channel didn't already exist, so create it and set the filters. */
+ channels[channel] = {
+ usernum: 1,
+ filters: users[session.nid].filters
+ };
+ }
+ });
+ socket.on('part', function() {
+ if (!users[session.nid].channel) {
+ return;
+ }
+ logAction("PART");
+ socket.leave(users[session.nid].channel);
+ if (!--channels[users[session.nid].channel].usernum) {
+ delete channels[users[session.nid].channel];
+ }
+ delete users[session.nid].channel;
+ io.sockets.emit('part', {
+ id: session.nid
});
- socket.on('disconnect', function() {
- if (ghosting) {
- logAction("GHOSTED");
- return;
- }
- logAction("DISCONNECT");
- delete sockets[session.nid];
- delete users[session.nid];
- socket.broadcast.emit('logout', {
- id: session.nid
- });
+ });
+ socket.on('filter', function(d) {
+ if (!(typeof(d) == "object" && "filters" in d)) {
+ return;
+ }
+ users[session.nid].filters = d.filters;
+ logAction("FILTER", d.filters);
+ var channel = users[session.nid].channel;
+ if (!channel) {
+ return;
+ }
+ channels[channel].filters = d.filters;
+ socket.broadcast.to(channel).emit('filterset', {
+ id: session.nid,
+ filters: d.filters
+ });
+ });
+ socket.on('disconnect', function() {
+ if (ghosting) {
+ logAction("GHOSTED");
+ return;
+ }
+ logAction("DISCONNECT");
+ delete sockets[session.nid];
+ delete users[session.nid];
+ socket.broadcast.emit('logout', {
+ id: session.nid
});
});
});
diff --git a/public/js/mv/connect.js b/public/js/mv/connect.js
index 6e521a3..39b2f55 100644
--- a/public/js/mv/connect.js
+++ b/public/js/mv/connect.js
@@ -16,7 +16,7 @@ var mv = function(mv) {
/* io.socket's socket */
var socket;
function connect() {
- socket = io.connect('http://localhost:3000');
+ socket = io.connect();
/* These are still useful to troubleshoot */
socket.on("connect", function() {
console.log("CONNECT", arguments);
@@ -27,7 +27,6 @@ var mv = function(mv) {
});
/* We're evidently operating online, so show the status */
d3.select("#connect-status").style("display", "block");
- socket.emit('login');
/* Tell the server our starting filters */
socket.emit("filter", { filters: mv.charter.filters() });
/*