簡體   English   中英

dnode服務器->客戶端直接調用可以嗎? node.js

[英]dnode server->client direct call possible? node.js

我用dnode

我已閱讀StackOverflow: 使用dnode從服務器向客戶端發送消息

並理解

dnode使用對稱協議,因此任一端都可以定義相對端可以調用的函數。

作者@substack回答。

所以,現在,我有如下代碼:

server.js

var HTTPserver = httpServer('/www')
            .listen(9999, function()
            {
                console.log('HTTP listening 9999');
            });

        var dnode = require('dnode');

        var shoe = require('shoe')(
            function(stream)
            {
                var TCPserver = require('net')
                    .createServer()
                    .listen(5005, function()
                    {
                        console.log('TCP listening 5005');
                    })
                    .on('connection', function(socket)
                    {
                        console.log('TCPsocket connected');
                        var d = dnode(
                        {
                        });
                        d.on('remote', function(remote)
                        {
                            remote.test();
                        });
                        d
                            .pipe(stream)
                            .pipe(d);

                        socket.end();
                    })
                    .on('end', function()
                    {
                        console.log('TCPsocket  disconnected');
                    });

            })
            .install(HTTPserver, '/dnode');

client.js

var shoe = require('shoe');
        var stream = shoe('/dnode');
        var dnode = require('dnode');
        var d = dnode(
        {
            test: function()
            {
                console.log('hello');
            }
        });
        d.on('remote', function(remote)
        {
            console.log('connnected');
        });
        d.pipe(stream)
            .pipe(d);

基本上,我想調用function:test-> hello從服務器啟動。

但是,我看到的結果是

d.on('remote', function(remote) { console.log('connnected'); });

@客戶端被評估。

d.on('remote', function(remote) { remote.test(); });

@服務器從不評估。

這是為什么?

當然,也許我可以解決使用client-> server-> client回調方法的問題,但是如果可能的話,我只是想為以后的工作提供直接的方法。

謝謝。

我發現答案很簡單,在TCP套接字回調中生成事件定義中的dnode d.on是個壞主意,因為在尚未觸發dnode之前,還沒有dnode東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM