簡體   English   中英

Node.js socket.io 服務器的客戶端

[英]Node.js client for a socket.io server

我有一個 socket.io 服務器正在運行,並且有一個匹配的網頁與 socket.io.js 客戶端。 一切正常。

但是,我想知道是否有可能在另一台機器上運行一個單獨的 node.js 應用程序作為客戶端並連接到提到的 socket.io 服務器?

這應該可以使用 Socket.IO-client: https://github.com/LearnBoost/socket.io-client

添加之前給出的解決方案的示例。 通過使用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

安裝 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

是的,只要 socket.io 支持,您就可以使用任何客戶端。 無論是 node、java、android 還是 swift。 您所要做的就是安裝 socket.io 的客戶端包。

客戶端代碼:我有一個要求,我的 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);
        });

這樣的事情對我有用

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);
});
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", {});
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM