Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用dnode从服务器向客户端发送消息_Javascript_Node.js_Nowjs Sockets_Dnode - Fatal编程技术网

Javascript 使用dnode从服务器向客户端发送消息

Javascript 使用dnode从服务器向客户端发送消息,javascript,node.js,nowjs-sockets,dnode,Javascript,Node.js,Nowjs Sockets,Dnode,几个月前,我发现了nowjs和dnode,并最终将nowjs(and)用于客户机/服务器双向通信 nowclient支持2个节点进程之间的nowjs通信(而不是节点进程和nowjs现成浏览器之间的通信)。然后,我能够将数据从客户端发送到服务器,并从服务器发送到客户端。我现在使用节点0.6.12,使用节点0.4.x运行客户端有点痛苦 我正在仔细研究dnode,但我不确定服务器到客户端的通信是如何工作的。服务器是否可能向客户端发送直接消息?其想法是让客户机在服务器上注册(在第一次连接时),并允许服务

几个月前,我发现了nowjs和dnode,并最终将nowjs(and)用于客户机/服务器双向通信

nowclient支持2个节点进程之间的nowjs通信(而不是节点进程和nowjs现成浏览器之间的通信)。然后,我能够将数据从客户端发送到服务器,并从服务器发送到客户端。我现在使用节点0.6.12,使用节点0.4.x运行客户端有点痛苦

我正在仔细研究dnode,但我不确定服务器到客户端的通信是如何工作的。服务器是否可能向客户端发送直接消息?其想法是让客户机在服务器上注册(在第一次连接时),并允许服务器在需要时联系客户机


据我所知,如果客户机首先从服务器请求了一些东西,那么在服务器上调用方法是可能的。正确吗?

dnode使用对称协议,因此任何一方都可以定义对方可以调用的函数。您可以采取两种基本方法

第一种方法是在服务器端定义一个register函数,并从客户端传入回调

服务器:

var dnode = require('dnode');

dnode(function (remote, conn) {
    this.register = function (cb) {
        // now just call `cb` whenever you like!
        // you can call cb() with whichever arguments you like,
        // including other callbacks!

        setTimeout(function () {
            cb(55);
        }, 1337);
    };
}).listen(5000)
var dnode = require('dnode');

dnode.connect('localhost', 5000, function (remote, conn) {
    remote.register(function (x) {
        console.log('the server called me back with x=' + x);
    });
});
var dnode = require('dnode');

dnode(function (remote, conn) {
    conn.on('ready', function () {
        remote.foo(55);
    });
}).listen(5000);
var dnode = require('dnode');
dnode(function (remote, conn) {
    this.foo = function (n) {
        console.log('the server called me back with n=' + n);
    };
}).connect('localhost', 5000);
客户端:

var dnode = require('dnode');

dnode(function (remote, conn) {
    this.register = function (cb) {
        // now just call `cb` whenever you like!
        // you can call cb() with whichever arguments you like,
        // including other callbacks!

        setTimeout(function () {
            cb(55);
        }, 1337);
    };
}).listen(5000)
var dnode = require('dnode');

dnode.connect('localhost', 5000, function (remote, conn) {
    remote.register(function (x) {
        console.log('the server called me back with x=' + x);
    });
});
var dnode = require('dnode');

dnode(function (remote, conn) {
    conn.on('ready', function () {
        remote.foo(55);
    });
}).listen(5000);
var dnode = require('dnode');
dnode(function (remote, conn) {
    this.foo = function (n) {
        console.log('the server called me back with n=' + n);
    };
}).connect('localhost', 5000);
或者,在方法交换完成后,您可以直接从服务器以对称方式调用客户端:

服务器:

var dnode = require('dnode');

dnode(function (remote, conn) {
    this.register = function (cb) {
        // now just call `cb` whenever you like!
        // you can call cb() with whichever arguments you like,
        // including other callbacks!

        setTimeout(function () {
            cb(55);
        }, 1337);
    };
}).listen(5000)
var dnode = require('dnode');

dnode.connect('localhost', 5000, function (remote, conn) {
    remote.register(function (x) {
        console.log('the server called me back with x=' + x);
    });
});
var dnode = require('dnode');

dnode(function (remote, conn) {
    conn.on('ready', function () {
        remote.foo(55);
    });
}).listen(5000);
var dnode = require('dnode');
dnode(function (remote, conn) {
    this.foo = function (n) {
        console.log('the server called me back with n=' + n);
    };
}).connect('localhost', 5000);
客户端:

var dnode = require('dnode');

dnode(function (remote, conn) {
    this.register = function (cb) {
        // now just call `cb` whenever you like!
        // you can call cb() with whichever arguments you like,
        // including other callbacks!

        setTimeout(function () {
            cb(55);
        }, 1337);
    };
}).listen(5000)
var dnode = require('dnode');

dnode.connect('localhost', 5000, function (remote, conn) {
    remote.register(function (x) {
        console.log('the server called me back with x=' + x);
    });
});
var dnode = require('dnode');

dnode(function (remote, conn) {
    conn.on('ready', function () {
        remote.foo(55);
    });
}).listen(5000);
var dnode = require('dnode');
dnode(function (remote, conn) {
    this.foo = function (n) {
        console.log('the server called me back with n=' + n);
    };
}).connect('localhost', 5000);

谢谢你的澄清。客户端是否可以连接到服务器并等待来自服务器的传入呼叫?还有,是否有一个客户端标识符可以用于服务器端?我使用conn.id和remote来跟踪客户端。这真是太好了。我才意识到这个答案来自于他自己。是的,他很厉害!我试图弄清楚如何使用udp流来实现这一点,但事件发生在初始加载之后。似乎所有的例子都是针对第一次加载时发生的通信(d.on())。你们中有人知道如何在后续事件中这样做吗?