nwjs / nw.js

Call all Node.js modules directly from DOM/WebWorker and enable a new way of writing applications with all Web technologies.
https://nwjs.io
MIT License
40.38k stars 3.88k forks source link

in manifest use node-main,node-main will be run tow time(nw 0.14.7) #5913

Open bushadam opened 7 years ago

bushadam commented 7 years ago

my application need use activex, so i use child_process with C# application do this。 i find same issues https://github.com/nwjs/nw.js/issues/3818 ,but no answer。

my csharpapplication.exe will two instance,i find the app.js run tow times. one time on nw start,next time may be on document.onready or window.onload

nw version 0.14.7 x86 os windows xp(my customer need xp)

please help me thx

my manifest

{ "main": "index.html", "name": "", "description": "", "version": "0.1.0", "keywords": [ "ssdf", "node-webkit" ], "nodejs": true, "node-main": "js/app.js", "chromium-args": "--disable-web-security --disable-print-preview --mixed-context --enable-transparent-visuals --disable-gpu", "window": { "title": "ssdf", "icon": "app.ico", "toolbar": false, "resizable": false, "fullscreen": true, "show_in_taskbar": true, "frame": true, "show": true, "kiosk": true, "transparent": false, "always_on_top": false //"position": "center", //"width": 800, //"height": 670, //"min_width": 400, //"min_height": 335, //"max_width": 800, //"max_height": 670, }, "webkit": { "plugin": true, "page-cache": false } }

my app.js like this

` (function () { var log4js = require("log4js"); var iconv = require('iconv-lite');

var log4js_config = require("../config/log4js.json");
log4js.configure(log4js_config);
var logger = log4js.getLogger('app.js');

var killExplorer = function () {
    var exec = require('child_process').exec;
    exec('taskkill /f /im explorer.exe', function (error, stdout, stderr) {
        if (error) {
            logger.error('error:' + error);
        }
    });
};

var defaultCsharpOpt = {
    exe: 'csharpapplication.exe',
    closeCallback: function (exitcode) {},
    autoReOpen: false
};
var childprocess = null;

var open = function () {
    var spawn = require("child_process").spawn;
    childprocess = spawn(defaultCsharpOpt.exe);
    childprocess.stdin.setEncoding("binary");
    childprocess.stdout.setEncoding("binary");
    if (childprocess == null)
        if (opt.closeCallback)
            opt.closeCallback(-1);
    childprocess.on('exit', function (code) {
        if (opt.autoReOpen && code != 0 && !me.trueClose)
            setTimeout(function () {
                me.open();
            }, 2000);
        if (opt.closeCallback)
            opt.closeCallback(code);
    });

    childprocess.stdout.on('data', function (data) {
        //do somethings
    });
};

var close = function () {
    childprocess.kill();
};

var send = function (data, callback) {
    if (!childprocess && callback)
        return callback('no childprocess runing');
    if (!childprocess.stdin.writable && callback)
        return callback('childprocess stdin need writeable');

    if (callback){
        //do somethings
    }
    var message = iconv.encode(JSON.stringify(data) + "\r\n", 'gbk');
    childprocess.stdin.write(message);
};

killExplorer()
open();

//testing
// for(var i=0;i<2;i++){
    // send({command:"print",data:"11111"})
// }

exports.doPrint = function(sNo){
    send({command:"print",data:sNo})
};

// using in window.onload
exports.openDebug = function () {
    var win = nw.Window.get();
    win.showDevTools();
}

})(); `

Christywl commented 7 years ago

Please use English, so all users can read, or the issue will be closed directly.

bushadam commented 7 years ago

i have modify to English,but my English very poor,thx please help me

Christywl commented 7 years ago

I use the code in #3818, can't reproduce the issue on Linux/Windows 8 with nwjs-sdk-v0.22.3. (I have no Windows xp). I just see one line in the log.txt. package.json:

{
 "name": "test",
 "main": "index.html",
 "node-main": "app.js"
}

app.js:

var log4js = require('log4js');
log4js.configure({
    "appenders": [
        {
            type: "console"
        },
        {
            type:"file",
            filename:'log.txt'          
        }
    ],
    replaceConsole: true
});
var logger = global.logger;
if(!logger){
    global.logger = logger = log4js.getLogger('app');
    logger.debug('Here is a log');
}

I can't run with your code in the bug, you may upload your full tests for me to double check.

bushadam commented 7 years ago

@Christywl thx i use the same code with u,manifest is my, when i start app like this “nw appdir”,the log.txt will write two line [2017-05-19 10:59:36.806] [DEBUG] app - Here is a log [2017-05-19 10:59:37.087] [DEBUG] app - Here is a log when in devtool press “F5”, the log.txt content add one line,like this [2017-05-19 10:59:36.806] [DEBUG] app - Here is a log [2017-05-19 10:59:37.087] [DEBUG] app - Here is a log [2017-05-19 11:01:04.753] [DEBUG] app - Here is a log

that‘s the reason i just find at "chromium-args": "--disable-web-security --disable-print-preview --enable-transparent-visuals --disable-gpu",// --mixed-context,when i add the mixed-context,node-main will write two line,remove --mixed-context will write one line.

yesterday on my customer machine,i find another question.

when i call child_process like this require("child_process").spawn,the app crash,but on my dev machine no this problem.

my dev machine is virtual machine(windows xp sp3 32bit) on win10 64bit my customer machine is windows xp sp3 32bit, and cpu is 32bit.

@Christywl thx

Christywl commented 7 years ago

I can reproduce this issue when adding "chromium-args":"--mixed-context" on nwjs-sdk-v0.26.0.