与node.jssubprocess共享UDP套接字/端口

共享一个TCP套接字/端口很容易使用node.js集群,但似乎不可能用UDP dgram做到这一点。

有没有办法做到这一点或者使用群集,共享进程之间的文件描述符或其他?

我有很多问题,因为node.js并不真正“分叉”,它“产生”或“fork / execs”。 当我使用群集的UDP服务器时,只有一个subprocess会接收数据包,最后一个绑定。 如果你只是简单的“fork()”,操作系统就会将传入的数据包解读给每个孩子。 如果你“spawn()”遇到了文件/套接字句柄的inheritance权限问题,必须设置选项等,而底层的node.js udp服务器可能没有应用这些选项。

我不得不写我自己的扩展名,简单地称为底层操作系统fork(),并使其工作就像一个普通的分叉,networking服务器。

Windows没有fork(),所以这种方法将无法工作,这可能是为什么node.js没有一个普通的,简单的,花园多种fork()。 这样做会使其不能移植到Windows。

1)创build一个目录,我称之为“util”。

2)把这两个文件放在那个目录下。

——————-在这里切断,命名如下“util.cc”——-

#include <v8.h> //needed for extension infrastructure #include <node.h> //needed for extension infrastructure #include <iostream> // not part of extension infrastructure, just for the code I'm adding and only while developing to output debugging messages using namespace node; using namespace v8; // The following two functions are examples of the minimum required for a node.js extension that does anything static Handle<Value> spoon(const Arguments& args) { pid_t rval = fork(); if (rval < 0) { return ThrowException(Exception::Error(String::New("Unable to fork daemon, pid < 0."))); } Handle<Value> n = v8::Number::New(rval); return n; } static Handle<Value> pid(const Arguments& args) { pid_t rval = getpid(); Handle<Value> n = v8::Number::New(rval); return n; } extern "C" void init(Handle<Object> target) { NODE_SET_METHOD(target, "fork", spoon); NODE_SET_METHOD(target, "pid", pid); } 

——–切入这里,命名如下“wscript”——-

 def set_options(opt): opt.tool_options("compiler_cxx") def configure(conf): conf.check_tool("compiler_cxx") conf.check_tool("node_addon") def build(bld): obj = bld.new_task_gen("cxx", "shlib", "node_addon") obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"] obj.target = "util" obj.source = "util.cc" 

———切割结束,留给我们切刀——

3)运行“node-waf configure”

如果这样顺利,

4)运行“node-waf”

5)将创build一个名为“build”的新目录,并且您的扩展名“build / default / util.node”将被创build。 复制该地址,并从节点程序中使用它,如:

var util = require(“util.node”);

var pid = util.fork();

还包含一个util.pid()函数,因为分叉后process.pid不能正常工作。 它提供了父进程的PID。

我是一个初学者的节点扩展作家,所以如果这是一个天真的方法,哦,但它迄今为止服务得很好。 任何改进,如“简化”将不胜感激。

在node.js v0.10中解决了这个问题