mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-04-02 14:29:37 +07:00
100 lines
2.2 KiB
JavaScript
100 lines
2.2 KiB
JavaScript
|
|
var tcpServer;
|
|
var commandWindow;
|
|
|
|
/**
|
|
* Listens for the app launching then creates the window
|
|
*
|
|
* @see https://developer.chrome.com/apps/app_runtime
|
|
* @see https://developer.chrome.com/apps/app_window
|
|
*/
|
|
chrome.app.runtime.onLaunched.addListener(function() {
|
|
if (commandWindow && !commandWindow.contentWindow.closed) {
|
|
commandWindow.focus();
|
|
} else {
|
|
chrome.app.window.create('index.html',
|
|
{id: "mainwin", innerBounds: {width: 500, height: 309, left: 0}},
|
|
function(w) {
|
|
commandWindow = w;
|
|
});
|
|
}
|
|
});
|
|
|
|
|
|
// event logger
|
|
var log = (function(){
|
|
var logLines = [];
|
|
var logListener = null;
|
|
|
|
var output=function(str) {
|
|
if (str.length>0 && str.charAt(str.length-1)!='\n') {
|
|
str+='\n'
|
|
}
|
|
logLines.push(str);
|
|
if (logListener) {
|
|
logListener(str);
|
|
}
|
|
};
|
|
|
|
var addListener=function(listener) {
|
|
logListener=listener;
|
|
// let's call the new listener with all the old log lines
|
|
for (var i=0; i<logLines.length; i++) {
|
|
logListener(logLines[i]);
|
|
}
|
|
};
|
|
|
|
return {output: output, addListener: addListener};
|
|
})();
|
|
|
|
|
|
|
|
function onAcceptCallback(tcpConnection, socketInfo) {
|
|
var info="["+socketInfo.peerAddress+":"+socketInfo.peerPort+"] Connection accepted!";
|
|
log.output(info);
|
|
console.log(socketInfo);
|
|
tcpConnection.addDataReceivedListener(function(data) {
|
|
var lines = data.split(/[\n\r]+/);
|
|
for (var i=0; i<lines.length; i++) {
|
|
var line=lines[i];
|
|
if (line.length>0) {
|
|
var info="["+socketInfo.peerAddress+":"+socketInfo.peerPort+"] "+line;
|
|
log.output(info);
|
|
|
|
var cmd=line.split(/\s+/);
|
|
try {
|
|
tcpConnection.sendMessage(Commands.run(cmd[0], cmd.slice(1)));
|
|
} catch (ex) {
|
|
tcpConnection.sendMessage(ex);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
function startServer(addr, port) {
|
|
if (tcpServer) {
|
|
tcpServer.disconnect();
|
|
}
|
|
tcpServer = new TcpServer(addr, port);
|
|
tcpServer.listen(onAcceptCallback);
|
|
}
|
|
|
|
|
|
function stopServer() {
|
|
if (tcpServer) {
|
|
tcpServer.disconnect();
|
|
tcpServer=null;
|
|
}
|
|
}
|
|
|
|
function getServerState() {
|
|
if (tcpServer) {
|
|
return {isConnected: tcpServer.isConnected(),
|
|
addr: tcpServer.addr,
|
|
port: tcpServer.port};
|
|
} else {
|
|
return {isConnected: false};
|
|
}
|
|
}
|