用于socket.io服务器的Node.js客户端

时间:2012-05-22 13:55:15

标签: node.js socket.io

我运行了一个socket.io服务器,并且有一个与socket.io.js客户端匹配的网页。一切正常。

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

6 个答案:

答案 0 :(得分:74)

应该可以使用Socket.IO-client:https://github.com/LearnBoost/socket.io-client

答案 1 :(得分:35)

在前面给出的解决方案中添加示例。使用socket.io-client https://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

答案 2 :(得分:8)

安装socket.io-client后:

npm install socket.io-client

这就是客户端代码的样子:

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

感谢alessioalex

答案 3 :(得分:0)

是的,只要socket.io支持,您就可以使用任何客户端。无论其节点是Java,android还是swift。您要做的就是安装socket.io的客户端软件包。

答案 4 :(得分:0)

客户端代码:我有一个条件,要求我的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);
        });

答案 5 :(得分:0)

这样的东西对我有用

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