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) {
sio.ioServers.forEach(function (io) {
io.sockets.in(ns).emit(msg, data);
});
sio.instance.in(ns).emit(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.
*/
function handleAuth(data, accept) {
data.user = false;
function handleAuth(socket, accept) {
var data = socket.request;
socket.user = false;
if (data.headers.cookie) {
cookieParser(data, null, function () {
var auth = data.cookies.auth;
db.users.verifyAuth(auth, function (err, user) {
if (!err) {
data.user = {
socket.user = {
name: user.name,
global_rank: user.global_rank
};
@ -124,8 +126,14 @@ function addTypecheckedFunctions(sock) {
* Called after a connection is accepted
*/
function handleConnection(sock) {
var ip = sock.handshake.address.address;
var sockUser = sock.handshake.user;
var ip = sock.client.conn.remoteAddress;
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)) {
ip = util.expandIPv6(ip);
}
@ -159,10 +167,10 @@ function handleConnection(sock) {
addTypecheckedFunctions(sock);
var user = new User(sock);
if (sockUser) {
if (sock.user) {
user.setFlag(Flags.U_REGISTERED);
user.clearFlag(Flags.U_READY);
user.refreshAccount({ name: sockUser.name },
user.refreshAccount({ name: sock.user.name },
function (err, account) {
if (err) {
user.clearFlag(Flags.U_REGISTERED);
@ -190,44 +198,30 @@ function handleConnection(sock) {
module.exports = {
init: function (srv) {
var bound = {};
var io = sio.instance = sio();
io.use(handleAuth);
io.on("connection", handleConnection);
Config.get("listen").forEach(function (bind) {
if (!bind.io) {
return;
}
var id = bind.ip + ":" + bind.port;
if (id in srv.ioServers) {
if (id in bound) {
Logger.syslog.log("[WARN] Ignoring duplicate listen address " + id);
return;
}
var io = null;
if (id in srv.servers) {
io = srv.ioServers[id] = sio.listen(srv.servers[id]);
io.attach(srv.servers[id]);
} else {
if (net.isIPv6(bind.ip) || 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);
}
io.attach(require("http").createServer().listen(bind.port, bind.ip));
}
if (io) {
io.set("log level", 1);
io.set("authorization", handleAuth);
io.on("connection", handleConnection);
}
bound[id] = null;
});
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 db = require("./database");
var Flags = require("./flags");
var sio = require("socket.io");
var Server = function () {
var self = this;
@ -55,7 +56,6 @@ var Server = function () {
self.announcement = null;
self.infogetter = null;
self.servers = {};
self.ioServers = {};
// database init ------------------------------------------------------
var Database = require("./database");
@ -207,9 +207,7 @@ Server.prototype.announce = function (data) {
} else {
this.announcement = data;
db.setAnnouncement(data);
for (var id in this.ioServers) {
this.ioServers[id].sockets.emit("announcement", data);
}
sio.instance.emit("announcement", data);
}
};

View File

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

View File

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