From 13875e6a149779e77dcfe183a4010bf88847b721 Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Thu, 2 Mar 2017 16:26:22 -0500 Subject: [PATCH] debugging --- index.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 7d0a71a..f559b33 100644 --- a/index.js +++ b/index.js @@ -21,14 +21,7 @@ var Buffer = require('buffer').Buffer // to the right handlers. var servers = {} var sockets = {} - -// Thorough check for Chrome App since both Edge and Chrome implement dummy chrome object -if (typeof chrome === 'object' && typeof chrome.runtime === 'object' && typeof chrome.runtime.id === 'string') { - chrome.sockets.tcpServer.onAccept.addListener(onAccept) - chrome.sockets.tcpServer.onAcceptError.addListener(onAcceptError) - chrome.sockets.tcp.onReceive.addListener(onReceive) - chrome.sockets.tcp.onReceiveError.addListener(onReceiveError) -} +var listenersAdded = false function onAccept (info) { if (info.socketId in servers) { @@ -138,6 +131,17 @@ function Server (options, connectionListener) { if (!(this instanceof Server)) return new Server(options, connectionListener) EventEmitter.call(this) + if(!listenersAdded) { + // Thorough check for Chrome App since both Edge and Chrome implement dummy chrome object + if (typeof chrome === 'object' && typeof chrome.runtime === 'object') { + chrome.sockets.tcpServer.onAccept.addListener(onAccept) + chrome.sockets.tcpServer.onAcceptError.addListener(onAcceptError) + chrome.sockets.tcp.onReceive.addListener(onReceive) + chrome.sockets.tcp.onReceiveError.addListener(onReceiveError) + listenersAdded = true + } + } + if (typeof options === 'function') { connectionListener = options options = {} @@ -516,6 +520,17 @@ inherits(Socket, stream.Duplex) function Socket (options) { if (!(this instanceof Socket)) return new Socket(options) + if(!listenersAdded) { + // Thorough check for Chrome App since both Edge and Chrome implement dummy chrome object + if (typeof chrome === 'object' && typeof chrome.runtime === 'object') { + chrome.sockets.tcpServer.onAccept.addListener(onAccept) + chrome.sockets.tcpServer.onAcceptError.addListener(onAcceptError) + chrome.sockets.tcp.onReceive.addListener(onReceive) + chrome.sockets.tcp.onReceiveError.addListener(onReceiveError) + listenersAdded = true + } + } + if (typeof options === 'number') { options = { fd: options } // Legacy interface. } else if (options === undefined) {