Skip to content

Commit

Permalink
[wip] - proof of concept to fix #625
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Jul 26, 2015
1 parent 8079ffe commit cf8f994
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/async-tasks.js
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions lib/async.js
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/connect-utils.js
Expand Up @@ -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) {
Expand All @@ -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);
},
Expand Down
3 changes: 3 additions & 0 deletions lib/default-config.js
Expand Up @@ -377,6 +377,9 @@ module.exports = {
* @type Object
*/
socket: {
socketIoOptions: {
log: false,
},
path: "/browser-sync/socket.io",
clientPath: "/browser-sync",
namespace: "/browser-sync",
Expand Down
4 changes: 2 additions & 2 deletions lib/server/proxy-server.js
Expand Up @@ -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;
};
Expand Down
20 changes: 17 additions & 3 deletions lib/sockets.js
@@ -1,6 +1,7 @@
"use strict";

var socket = require("socket.io");
var utils = require("./server/utils");
var Steward = require("emitter-steward");

/**
Expand All @@ -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);
Expand Down

0 comments on commit cf8f994

Please sign in to comment.