diff --git a/plugins/c9.cli.bridge/bridge-service.js b/plugins/c9.cli.bridge/bridge-service.js index 77e900c7..aeafc670 100644 --- a/plugins/c9.cli.bridge/bridge-service.js +++ b/plugins/c9.cli.bridge/bridge-service.js @@ -10,7 +10,7 @@ module.exports = function (vfs, options, register) { function createListenClient(api){ var client = net.connect(SOCKET, function(data){ - if (data) api.onData(data); + api.onConnect(client); }); client.setEncoding("utf8"); client.unref(); @@ -36,7 +36,6 @@ module.exports = function (vfs, options, register) { createListenServer(api); }); - api.onConnect(client); api.disconnect = function(){ client.end(); @@ -45,24 +44,46 @@ module.exports = function (vfs, options, register) { return client; } - function createListenServer(api){ - // var timeout = setTimeout(function(){ - // unixServer.close(); - // }, 500); - - var unixServer = net.createServer(function(client) { - client.setEncoding("utf8"); + function createListenServer(api){ + function broadcast(data, client) { + clients.forEach(function(c) { + if (c != client) + c.write(data); + }); + } + function registerClient(client) { + if (client.setEncoding) + client.setEncoding("utf8"); client.on("data", function(data){ - if (data) api.onData(data); + // TODO add a way for sending message to one client + broadcast(data, client); }); + function cleanup(e) { + var i = clients.indexOf(client); + if (i != -1) + clients.splice(i, 1); + + client.removeListener("end", cleanup); + client.removeListener("error", cleanup); + } + client.on("end", cleanup); + client.on("error", cleanup); - client.on("error", function(data){ - // console.error("ERROR", api.id, data); - }); - - api.onConnect(client); - }); + + clients.push(client); + } + + api + var clients = []; + var stream = new Stream(); + stream.readable = true; + stream.writable = true; + stream.write = function(e) { + api.onData(e); + }; + registerClient(stream); + var unixServer = net.createServer(registerClient); unixServer.listen(SOCKET); unixServer.on("error", function(err){ @@ -85,6 +106,9 @@ module.exports = function (vfs, options, register) { stream = new Stream(); stream.readable = true; stream.writable = true; + stream.write = function(data){ + if (client) client.write(data); + }; var client; var sent = false; @@ -107,10 +131,6 @@ module.exports = function (vfs, options, register) { // createListenServer createListenClient(api); - - stream.write = function(data){ - if (client) client.write(data); - }; }, disconnect: function(){ diff --git a/plugins/c9.cli.bridge/bridge_commands.js b/plugins/c9.cli.bridge/bridge_commands.js index 55f5ad76..1952f719 100644 --- a/plugins/c9.cli.bridge/bridge_commands.js +++ b/plugins/c9.cli.bridge/bridge_commands.js @@ -40,7 +40,8 @@ define(function(require, exports, module) { e.respond(null, true); break; default: - console.error("Unknown Bridge Command: ", message.type); + if (message.type) + console.error("Unknown Bridge Command: ", message.type); break; } }, plugin);