Question

I use dnode

I have read StackOverflow: Send message from server to client with dnode

and undersand

dnode uses a symmetric protocol so either side can define functions that the opposing side can call.

as @substack the author replied.

So, right now, I have a code as the below:

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);

Basically, I want to call function:test -> hello initiated from server.

However, the result I see is

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

@ client is evaluated.

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

@ server is never evaluated.

Why is that?

Of course, probably I can work around using client->server->client call back method, but if possible I just would like the straight forward way for my future work.

Thanks.

Was it helpful?

Solution

I found the answer which is simple enough, it is a bad idea to generate dnode d.on event definition in the TCP socket call back, since before dnode stuff is not there yet when it should triggered.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top