www.example.com服务器的Node.js客户端socket.io

ut6juiuv  于 2023-01-01  发布在  Node.js
关注(0)|答案(7)|浏览(151)

我有一socket.io运行的www.example.com服务器和一个与socket.io.js客户端匹配的网页。
但是,我想知道是否有可能在另一台机器上运行一个单独的node.js应用程序,该应用程序将充当客户端并连接到上述socket.io服务器?

ztmd8pv5

ztmd8pv52#

添加前面给出的解决方案的示例。通过使用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

ff29svar

ff29svar3#

安装套接字.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?' });

谢谢。

kknvjkwl

kknvjkwl4#

const io = require('socket.io-client');
const socket_url = "http://localhost:8081";

let socket = io.connect(socket_url);

socket.on('connect', function () {
    socket.emit("event_name", {});
});
pu82cl6c

pu82cl6c5#

是的,你可以使用任何客户端,只要它是由socket.io支持.无论是它的节点,java,android或swift.所有你需要做的是安装socket.io的客户端软件包.

sshcrbum

sshcrbum6#

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

pbgvytdp7#

像这样的东西对我很有效

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

相关问题