【问题标题】:Node.js client for a socket.io server用于 socket.io 服务器的 Node.js 客户端
【发布时间】:2012-05-29 00:45:24
【问题描述】:

我有一个正在运行的 socket.io 服务器和一个带有 socket.io.js 客户端的匹配网页。一切正常。

但是,我想知道是否有可能在另一台机器上运行一个单独的 node.js 应用程序,该应用程序将充当客户端并连接到提到的 socket.io 服务器?

【问题讨论】:

  • 如何查看来自 socket.emit() 的响应?
  • 查看github.com/LearnBoost/socket.io-client 文档,我确定它在那里。好久不见了,不记得了,抱歉……
  • 如果如您所述调用io.connect 函数,则它不起作用。它应该被称为:socket = io.connect('http://localhost:1337');

标签: node.js socket.io


【解决方案1】:

使用 Socket.IO-client 应该可以做到这一点:https://github.com/LearnBoost/socket.io-client

【讨论】:

  • 嗯,我可能弄错了,但这看起来像是在浏览器中运行的客户端。我需要的是一个独立的 node.js 客户端。
  • 我最近没有检查过,但是在 Node 0.4.x 中这也可以在服务器上运行(我实际上在过去的项目中实现了这个)。
  • 我很高兴它对你有用!顺便说一句,最好将您的工作示例放在问题上而不是单独的答案中。
  • 这在 Windows 8 上没有为我正确安装 - 我为它写了一个错误
  • @PredragStojadinović:你能发布你的代码吗?我想将一个 NodeJS 服务器连接到另一个。你能帮我吗?谢谢。
【解决方案2】:

安装socket.io-client后:

npm install socket.io-client

这是客户端代码的样子:

var io = require('socket.io-client'),
socket = io.connect('http://localhost', {
    port: 1337,
    reconnect: true
});
socket.on('connect', function () { console.log("socket connected"); });
socket.emit('private message', { user: 'me', msg: 'whazzzup?' });

谢谢alessioalex

【讨论】:

    【解决方案3】:

    为前面给出的解决方案添加示例。通过使用socket.io-clienthttps://github.com/socketio/socket.io-client

    客户端:

    //client.js
    var io = require('socket.io-client');
    var socket = io.connect('http://localhost:3000', {reconnect: true});
    
    // Add a connect listener
    socket.on('connect', function (socket) {
        console.log('Connected!');
    });
    socket.emit('CH01', 'me', 'test msg');
    

    服务器端:

    //server.js
    var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    
    io.on('connection', function (socket){
       console.log('connection');
    
      socket.on('CH01', function (from, msg) {
        console.log('MSG', from, ' saying ', msg);
      });
    
    });
    
    http.listen(3000, function () {
      console.log('listening on *:3000');
    });
    

    运行:

    打开2个控制台并运行node server.jsnode client.js

    【讨论】:

    • 很棒的例子!一件事,在客户端,我不相信“套接字”变量会在连接事件上传递。也许我错了,但这似乎是我在 npm socket.io-client 中看到的行为
    【解决方案4】:

    是的,只要 socket.io 支持,您可以使用任何客户端。不管是node、java、android还是swift。你只需要安装socket.io的客户端包。

    【讨论】:

      【解决方案5】:

      客户端代码:我有一个要求,我的 nodejs 网络服务器既可以作为服务器也可以作为客户端,所以当我需要它作为客户端时,我添加了下面的代码,它应该可以正常工作,我正在使用它并且可以正常工作我!!!

      const socket = require('socket.io-client')('http://192.168.0.8:5000', {
                  reconnection: true,
                  reconnectionDelay: 10000
                });
          
              socket.on('connect', (data) => {
                  console.log('Connected to Socket');
              });
              
              socket.on('event_name', (data) => {
                  console.log("-----------------received event data from the socket io server");
              });
          
              //either 'io server disconnect' or 'io client disconnect'
              socket.on('disconnect', (reason) => {
                  console.log("client disconnected");
                  if (reason === 'io server disconnect') {
                    // the disconnection was initiated by the server, you need to reconnect manually
                    console.log("server disconnected the client, trying to reconnect");
                    socket.connect();
                  }else{
                      console.log("trying to reconnect again with server");
                  }
                  // else the socket will automatically try to reconnect
                });
          
              socket.on('error', (error) => {
                  console.log(error);
              });
      

      【讨论】:

        【解决方案6】:

        这样的东西对我有用

        const WebSocket = require('ws');
        const ccStreamer = new WebSocket('wss://somthing.com');
        
        ccStreamer.on('open', function open() {
          var subRequest = {
            "action": "SubAdd",
            "subs": [""]
          };
          ccStreamer.send(JSON.stringify(subRequest));
        });
        
        ccStreamer.on('message', function incoming(data) {
          console.log(data);
        });
        

        【讨论】:

          猜你喜欢
          • 2015-10-21
          • 1970-01-01
          • 2020-04-07
          • 2013-10-30
          • 2019-03-31
          • 1970-01-01
          • 1970-01-01
          • 2021-11-18
          • 2015-03-25
          相关资源
          最近更新 更多