Node.js client for a socket.io server

前端 未结 5 1982
天涯浪人
天涯浪人 2020-11-28 20:22

I have a socket.io server running and a matching webpage with a socket.io.js client. All works fine.

But, I am wondering if it is possible, on another machine, to ru

相关标签:
5条回答
  • 2020-11-28 21:18

    After installing socket.io-client:

    npm install socket.io-client
    

    This is how the client code looks like:

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

    Thanks alessioalex.

    0 讨论(0)
  • 2020-11-28 21:23

    Adding in example for solution given earlier. By using socket.io-client https://github.com/socketio/socket.io-client

    Client Side:

    //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 Side :

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

    Run :

    Open 2 console and run node server.js and node client.js

    0 讨论(0)
  • 2020-11-28 21:25

    Client side code: I had a requirement where my nodejs webserver should work as both server as well as client, so i added below code when i need it as client, It should work fine, i am using it and working fine for me!!!

    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);
            });
    
    0 讨论(0)
  • 2020-11-28 21:26

    Yes you can use any client as long as it is supported by socket.io. No matter whether its node, java, android or swift. All you have to do is install the client package of socket.io.

    0 讨论(0)
  • 2020-11-28 21:27

    That should be possible using Socket.IO-client: https://github.com/LearnBoost/socket.io-client

    0 讨论(0)
提交回复
热议问题