Using socket.io without client embedded in html

2019-05-27 03:57发布

Every where I m searching, socket.io is used in two part: one client and one server.

The server is a node.js application, but everytime, the client is a html page embedding the client code.

Is there a way to do so without html? For example, to make multiple node.js application communicating.

EDIT:

The use case is, I have a "program" in node.js, which I would like to send logs on demand to a server application. I m struggling in implementing the client code in the app.

Ultimately, if the server can send short message to all or one app, it would be wonderful because it would allow me to direct every app from a web page hosted by the server.

._____. ._____. ._____.
| app | | app | | app |
|_____| |_____| |_____|
   |       |       |
   |_______|_______|
           |
           | Logs
           V
       .________. Command   ._____.
       | server |---------->| app |
       |________|           |_____|

1条回答
小情绪 Triste *
2楼-- · 2019-05-27 04:24

You can use socket.io-client module to communicate with socket.io server.

https://github.com/LearnBoost/socket.io-client

Example client code -

var io = require('socket.io-client'),
socket = io.connect('localhost', {
    port: 1337
});

socket.on('connect', function () { 
  console.log("socket connected"); 
});

socket.emit('news', { hello: 'world' });
查看更多
登录 后发表回答