简体   繁体   English

在Node.js中的两个独立运行进程之间发送消息

[英]Send message between two independent running processes in Node.js

I've got an Adobe AIR Application on the local machine that communicates with an remote node.js server script (socket-script.js) via socket connection. 我在本地计算机上有一个Adobe AIR应用程序,它通过套接字连接与远程node.js服务器脚本(socket-script.js)进行通信。 Furthermore i start a new node.js process through command line and send some additional arguments to a second server script (terminal-script.js). 此外,我通过命令行启动一个新的node.js进程,并向第二个服务器脚本(terminal-script.js)发送一些额外的参数。 Question: How can i send the arguments from the terminal-script.js to socket-script.js? 问题:如何将参数从terminal-script.js发送到socket-script.js? Afterwards the socket-script.js should broadcast the args to the AIR Application. 然后socket-script.js应该将args广播到AIR应用程序。 Anyone an idea how to connect the two independent running processes in Node.js? 任何人都知道如何连接Node.js中的两个独立运行进程? Thanks. 谢谢。

在此输入图像描述

Illustration link 插图链接

Use the server to communicate between processes: 使用服务器在进程之间进行通信:

socket-script.js 插座的script.js

var net = require('net');
var app = null;

var server = net.createServer(function(socket) { 
    socket.on('data', function(data){
        if(data.indexOf('terminal:') >-1){
            if(app){
                app.write(data);
            }
        } else if(data.indexOf('app:') >-1){
            app = socket;
        }
    });
});

terminal-script.js: 终端的script.js:

var net = require('net');
var client = net.connect({port: 9001}, function() { 
    client.write('terminal:' + process.argv[2]);
});

app: 应用:

var net = require('net');
var client = net.connect({port: 9001}, function() { 
    client.write('app:connect');
});

client.on('data', function(data){
    if(data.indexOf('terminal:') >-1){
        // got terminal data
    }
});

The only way that I conceive of to make this work is something like this: 我设想做这项工作的唯一方法是这样的:

1) You'll need to have terminal-script.js be listening on a socket. 1)你需要让terminal-script.js在套接字上监听。 Like so: 像这样:

var arguments = process.args.splice(2);
var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end(arguments[0]);
}).listen(8000, '127.0.0.1');

2) Just make a request from socket-script to the terminal script: 2)只需从socket脚本向终端脚本发出请求:

//somewhere in socket-script use this to grab the value from the terminal script.
var http = require('http');

var options = {
  host: 'terminal-script-host.com',
  port: '8000',
  path: '/'
};

var req = http.get(options, function(res) {
    res.on('data', function (data) {
         console.log('socket-script got the data from terminal-script: ' + data);
     });
});

Not sure if this helps. 不确定这是否有帮助。 But I can tell you that it would be nearly impossible to "inject" something into the socket-script from the terminal-script, not in a way that would work with the same request anyways. 但是我可以告诉你,从终端脚本“插入”一些东西几乎是不可能的,而不是以同样的方式处理相同的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM