Update to socket.io 1.0

This commit is contained in:
calzoneman 2014-08-19 22:57:28 -05:00
parent 12804d1052
commit 6e0735f3fe
4 changed files with 21 additions and 42 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
}; };
@ -159,10 +161,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 +192,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

@ -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.0.6",
"yamljs": "^0.1.5" "yamljs": "^0.1.5"
} }
} }

View file

@ -1106,17 +1106,10 @@ try {
throw false; throw false;
} }
if (NO_WEBSOCKETS || USEROPTS.altsocket) {
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 }); socket = io(IO_URL, { secure: true });
} else { } else {
socket = io.connect(IO_URL); socket = io(IO_URL);
} }
setupCallbacks(); setupCallbacks();
} catch (e) { } catch (e) {