From 0eec3cdd675bbf870e3a63d2bd45c5b7aaa1a0de Mon Sep 17 00:00:00 2001 From: life Date: Tue, 22 Sep 2015 14:39:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E6=98=BE=E7=A4=BA=E4=B8=8D?= =?UTF-8?q?=E5=87=BA=E7=9A=84=E6=83=85=E5=86=B5!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/leanote/desktop-app/issues/51 --- src/node_modules/server.js | 34 +++++++++++++++++++++++++++++----- src/public/js/app/page.js | 3 ++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/node_modules/server.js b/src/node_modules/server.js index fa127954..534099c8 100644 --- a/src/node_modules/server.js +++ b/src/node_modules/server.js @@ -5,6 +5,7 @@ var http = require('http'); var url = require('url'); var path = require('path'); var fs = require('fs'); +var Common = require('common'); // http server, 处理笔记图片 var Server = { @@ -86,13 +87,36 @@ var Server = { }); }); - server.listen(Evt.port); - console.log("Server runing at port: " + Evt.port + "."); + // kill 端口占用的pid + var child_process = require('child_process'); + function killPort(callback) { + if (Common.isWin()) { + // & EXIT 表示只循环一次 + // Leanote会有两个pid绑定端口, 另一个和electron相关, kill掉也会把自己kill掉 + var sh1 = 'FOR /F "tokens=4 delims= " %P IN (\'netstat -a -n -o ^| findstr :' + Evt.port + '\') DO (TaskKill.exe /F /PID %P) & Exit'; + var sh2 = 'FOR /F "tokens=5 delims= " %P IN (\'netstat -a -n -o ^| findstr :' + Evt.port + '\') DO (TaskKill.exe /F /PID %P) & Exit'; + child_process.exec(sh1, function () { + child_process.exec(sh2, callback); + }); + } + else { + var sh = 'kill -9 $(lsof -i:' + Evt.port + ' -t)'; + child_process.exec(sh, callback); + } + } + killPort(function (error, stdout, stderr) { + if (error !== null) { + console.log('kill port error: ' + error); + } - me.localUrl = Evt.localUrl; - me._started = true; + server.listen(Evt.port); + console.log("Server runing at port: " + Evt.port + "."); - me.server = server; + me.localUrl = Evt.localUrl; + me._started = true; + + me.server = server; + }); }, // 关闭服务 diff --git a/src/public/js/app/page.js b/src/public/js/app/page.js index 05f8d2f3..858ae4fd 100644 --- a/src/public/js/app/page.js +++ b/src/public/js/app/page.js @@ -1382,8 +1382,9 @@ function initPage(initedCallback) { function _init() { $(function() { + // 很奇怪, 当port被占用后, notebook不能获取, 其它的可以 // 获取笔记本 - Service.notebookService.getNotebooks(function(notebooks) { + NotebookService.getNotebooks(function(notebooks) { Notebook.renderNotebooks(notebooks); ok(); });