Pergunta

I am trying to create using nodes core only a message sent by one server to another server (not clients just server-to-server).

In fact the servers are both the same server message is sent between 2 apps that must not be linked together. one must receive a remote message from the other.

I am looking over all google things and node docs but can't make sense of a few parts of the code for sending.

The first server only should receive messages:

var net=require('net');

var server1=net.createServer(function(nets){
    nets.addListener('message',function(data){
        console.dir(data);
        });
    });
server1.listen(8000,'localhost');

The second server should only send messages:

var net=require('net');

var server2=net.createConnection(8000,'localhost',function(nets){
    nets.on('connect',function(){
        nets.write('message',{'a':1,'foo':'bar'});
        });
});

I am having trouble understanding the docs though, I defiantly need to use net because the 2 apps must not be attached to each other so eventEmitter is a no-no

Foi útil?

Solução

The node.js documentation has some easy examples of both a TCP server as well as a TCP client that you can tweak for your application-specific logic.

Also: the documentation for Socket shows what you can do with an incoming or outgoing socket connection. As you will see, you cannot write JSON directly with node core. You have to stringify it first: sock.write(JSON.stringify({ foo: 'bar' }));

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top