Merge pull request #406 from calzoneman/socket.io-1.0

Socket.io 1.0
This commit is contained in:
Calvin Montgomery 2014-10-25 12:35:03 -05:00
commit 5d74559278
5 changed files with 33 additions and 49 deletions

View File

@ -609,9 +609,7 @@ Channel.prototype.handleReadLog = function (user) {
}; };
Channel.prototype._broadcast = function (msg, data, ns) { Channel.prototype._broadcast = function (msg, data, ns) {
sio.ioServers.forEach(function (io) { sio.instance.in(ns).emit(msg, data);
io.sockets.in(ns).emit(msg, data);
});
}; };
Channel.prototype.broadcastAll = function (msg, data) { Channel.prototype.broadcastAll = function (msg, data) {

View File

@ -26,14 +26,16 @@ var ipCount = {};
/** /**
* Called before an incoming socket.io connection is accepted. * Called before an incoming socket.io connection is accepted.
*/ */
function handleAuth(data, accept) { function handleAuth(socket, accept) {
data.user = false; var data = socket.request;
socket.user = false;
if (data.headers.cookie) { if (data.headers.cookie) {
cookieParser(data, null, function () { cookieParser(data, null, function () {
var auth = data.cookies.auth; var auth = data.cookies.auth;
db.users.verifyAuth(auth, function (err, user) { db.users.verifyAuth(auth, function (err, user) {
if (!err) { if (!err) {
data.user = { socket.user = {
name: user.name, name: user.name,
global_rank: user.global_rank global_rank: user.global_rank
}; };
@ -124,8 +126,14 @@ function addTypecheckedFunctions(sock) {
* Called after a connection is accepted * Called after a connection is accepted
*/ */
function handleConnection(sock) { function handleConnection(sock) {
var ip = sock.handshake.address.address; var ip = sock.client.conn.remoteAddress;
var sockUser = sock.handshake.user; if (!ip) {
sock.emit("kick", {
reason: "Your IP address could not be determined from the socket connection. See https://github.com/Automattic/socket.io/issues/1737 for details"
});
return;
}
if (net.isIPv6(ip)) { if (net.isIPv6(ip)) {
ip = util.expandIPv6(ip); ip = util.expandIPv6(ip);
} }
@ -159,10 +167,10 @@ function handleConnection(sock) {
addTypecheckedFunctions(sock); addTypecheckedFunctions(sock);
var user = new User(sock); var user = new User(sock);
if (sockUser) { if (sock.user) {
user.setFlag(Flags.U_REGISTERED); user.setFlag(Flags.U_REGISTERED);
user.clearFlag(Flags.U_READY); user.clearFlag(Flags.U_READY);
user.refreshAccount({ name: sockUser.name }, user.refreshAccount({ name: sock.user.name },
function (err, account) { function (err, account) {
if (err) { if (err) {
user.clearFlag(Flags.U_REGISTERED); user.clearFlag(Flags.U_REGISTERED);
@ -190,44 +198,30 @@ function handleConnection(sock) {
module.exports = { module.exports = {
init: function (srv) { init: function (srv) {
var bound = {};
var io = sio.instance = sio();
io.use(handleAuth);
io.on("connection", handleConnection);
Config.get("listen").forEach(function (bind) { Config.get("listen").forEach(function (bind) {
if (!bind.io) { if (!bind.io) {
return; return;
} }
var id = bind.ip + ":" + bind.port; var id = bind.ip + ":" + bind.port;
if (id in srv.ioServers) { if (id in bound) {
Logger.syslog.log("[WARN] Ignoring duplicate listen address " + id); Logger.syslog.log("[WARN] Ignoring duplicate listen address " + id);
return; return;
} }
var io = null;
if (id in srv.servers) { if (id in srv.servers) {
io = srv.ioServers[id] = sio.listen(srv.servers[id]); io.attach(srv.servers[id]);
} else { } else {
if (net.isIPv6(bind.ip) || bind.ip === "::") { io.attach(require("http").createServer().listen(bind.port, bind.ip));
/**
* Socket.IO won't bind to a v6 address natively.
* Instead, we have to create a node HTTP server, bind it
* to the desired address, then have socket.io listen on it
*/
io = srv.ioServers[id] = sio.listen(
require("http").createServer().listen(bind.port, bind.ip)
);
} else {
io = srv.ioServers[id] = sio.listen(bind.port, bind.ip);
}
} }
if (io) { bound[id] = null;
io.set("log level", 1);
io.set("authorization", handleAuth);
io.on("connection", handleConnection);
}
}); });
sio.ioServers = Object.keys(srv.ioServers)
.filter(Object.hasOwnProperty.bind(srv.ioServers))
.map(function (k) { return srv.ioServers[k] });
} }
}; };

View File

@ -45,6 +45,7 @@ var User = require("./user");
var $util = require("./utilities"); var $util = require("./utilities");
var db = require("./database"); var db = require("./database");
var Flags = require("./flags"); var Flags = require("./flags");
var sio = require("socket.io");
var Server = function () { var Server = function () {
var self = this; var self = this;
@ -55,7 +56,6 @@ var Server = function () {
self.announcement = null; self.announcement = null;
self.infogetter = null; self.infogetter = null;
self.servers = {}; self.servers = {};
self.ioServers = {};
// database init ------------------------------------------------------ // database init ------------------------------------------------------
var Database = require("./database"); var Database = require("./database");
@ -207,9 +207,7 @@ Server.prototype.announce = function (data) {
} else { } else {
this.announcement = data; this.announcement = data;
db.setAnnouncement(data); db.setAnnouncement(data);
for (var id in this.ioServers) { sio.instance.emit("announcement", data);
this.ioServers[id].sockets.emit("announcement", data);
}
} }
}; };

View File

@ -20,7 +20,7 @@
"oauth": "^0.9.12", "oauth": "^0.9.12",
"q": "^1.0.1", "q": "^1.0.1",
"serve-static": "^1.5.3", "serve-static": "^1.5.3",
"socket.io": "~0.9.16", "socket.io": "^1.1.0",
"yamljs": "^0.1.5" "yamljs": "^0.1.5"
} }
} }

View File

@ -1106,18 +1106,12 @@ try {
throw false; throw false;
} }
if (NO_WEBSOCKETS || USEROPTS.altsocket) { var opts = { transports: ["websocket", "polling"] };
var i = io.transports.indexOf("websocket");
if (i >= 0) {
io.transports.splice(i, 1);
}
}
if (IO_URL === IO_URLS["ipv4-ssl"] || IO_URL === IO_URLS["ipv6-ssl"]) { if (IO_URL === IO_URLS["ipv4-ssl"] || IO_URL === IO_URLS["ipv6-ssl"]) {
socket = io.connect(IO_URL, { secure: true }); opts.secure = true;
} else { socket = io(IO_URL, { secure: true });
socket = io.connect(IO_URL);
} }
socket = io(IO_URL, opts);
setupCallbacks(); setupCallbacks();
} catch (e) { } catch (e) {
if (e) { if (e) {