From cf8f9944192545c28c4eeaddd9a8238753272289 Mon Sep 17 00:00:00 2001 From: Shane Osbourne Date: Sun, 26 Jul 2015 20:55:12 +0100 Subject: [PATCH] [wip] - proof of concept to fix #625 --- lib/async-tasks.js | 4 ++++ lib/async.js | 24 ++++++++++++++++++++++++ lib/connect-utils.js | 6 ++++-- lib/default-config.js | 3 +++ lib/server/proxy-server.js | 4 ++-- lib/sockets.js | 20 +++++++++++++++++--- 6 files changed, 54 insertions(+), 7 deletions(-) diff --git a/lib/async-tasks.js b/lib/async-tasks.js index 208ebe729..c0a1351ea 100644 --- a/lib/async-tasks.js +++ b/lib/async-tasks.js @@ -5,6 +5,10 @@ module.exports = [ step: "Finding an empty port", fn: async.getEmptyPort }, + { + step: "Getting an extra port for Proxy", + fn: async.getExtraPortForProxy + }, { step: "Checking online status", fn: async.getOnlineStatus diff --git a/lib/async.js b/lib/async.js index ef1028ff4..f7a8e3ff0 100644 --- a/lib/async.js +++ b/lib/async.js @@ -27,6 +27,28 @@ module.exports = { }); }); }, + getExtraPortForProxy: function (bs, done) { + + if (bs.options.get("mode") !== "proxy") { + return done(); + } + + var port = bs.options.get("port") + 1; + + require("portscanner").findAPortNotInUse(port, null, { + host: "localhost", + timeout: 1000 + }, function (err, port) { + if (err) { + return done(err); + } + done(null, { + options: { + socketPort: port + } + }); + }); + }, /** * Some features require an internet connection. * If the user did not provide either `true` or `false` @@ -160,6 +182,8 @@ module.exports = { */ startServer: function (bs, done) { + console.log(bs.options.toJS()); + var clientJs = bs.pluginManager.hook("client:js", { port: bs.options.get("port"), options: bs.options diff --git a/lib/connect-utils.js b/lib/connect-utils.js index 506665827..3dcefb709 100644 --- a/lib/connect-utils.js +++ b/lib/connect-utils.js @@ -91,13 +91,15 @@ var connectUtils = { var withHostnamePort = "'{protocol}' + location.hostname + ':{port}{ns}'"; var withHost = "'{protocol}' + location.host + '{ns}'"; var withDomain = "'{domain}{ns}'"; + var port = options.get("port"); // default use-case is server/proxy var string = withHost; - if (options.get("mode") === "snippet") { + if (options.get("mode") !== "server") { protocol = options.get("scheme") + "://"; string = withHostnamePort; + port = options.get("socketPort"); } if (socketOpts.domain) { @@ -109,7 +111,7 @@ var connectUtils = { return string .replace("{protocol}", protocol) - .replace("{port}", options.get("port")) + .replace("{port}", port) .replace("{domain}", socketOpts.domain) .replace("{ns}", namespace); }, diff --git a/lib/default-config.js b/lib/default-config.js index 2c6176070..6684cd6b5 100644 --- a/lib/default-config.js +++ b/lib/default-config.js @@ -377,6 +377,9 @@ module.exports = { * @type Object */ socket: { + socketIoOptions: { + log: false, + }, path: "/browser-sync/socket.io", clientPath: "/browser-sync", namespace: "/browser-sync", diff --git a/lib/server/proxy-server.js b/lib/server/proxy-server.js index ec634ad00..cb5dcbcdf 100644 --- a/lib/server/proxy-server.js +++ b/lib/server/proxy-server.js @@ -29,9 +29,9 @@ module.exports = function createProxyServer (bs, scripts) { bs.proxy.app.use(bs.options.getIn(["scriptPaths", "path"]), scripts); /** - * How best to handle websockets going forward? + * Also proxy upgrades for Web Socket support */ - //proxy.server.on("upgrade", app.handleUpgrade); + proxy.server.on("upgrade", bs.proxy.app.handleUpgrade); return proxy; }; diff --git a/lib/sockets.js b/lib/sockets.js index 71d171bc8..0d2635a80 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -1,6 +1,7 @@ "use strict"; var socket = require("socket.io"); +var utils = require("./server/utils"); var Steward = require("emitter-steward"); /** @@ -18,10 +19,23 @@ module.exports.plugin = function (server, clientEvents, bs) { */ module.exports.init = function (server, clientEvents, bs) { - var emitter = bs.events; - var socketConfig = bs.options.get("socket").toJS(); + var emitter = bs.events; - var io = socket.listen(server, {log: false, path: socketConfig.path}); + var socketConfig = bs.options + .get("socket") + .toJS(); + + var socketPort = bs.options.get("socketPort"); + + if (bs.options.get("mode") === "proxy") { + server = utils.getServer(null, bs.options).server; + server.listen(socketPort); + } + + var socketIoConfig = socketConfig.socketIoOptions; + socketIoConfig.path = socketConfig.path; + + var io = socket(server, socketIoConfig); // Override default namespace. io.sockets = io.of(socketConfig.namespace);