PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 21, 2022

[FIXED] How to configure socket io and socket io client

 April 21, 2022     command-line, connection, node.js, sockets, unit-testing     No comments   

Issue

Good day

I need to connect a lot of pc's to a main server, through a server of units

hierarchy

I have something but I don't have all complete

Main Server

socketIo = require("socket.io"),
ioServer = socketIo(server),
ioServer.sockets.on("connection",function(socket){
     // Display a connected message
     console.log("Server-Client Connected!");
     // When we receive a message...
     socket.on("message",function(data){
         // We got a message... I dunno what we should do with this...
         console.log(data);
         console.log(data.from + " is connected with ip " + data.ip);
         socket.emit('message', { 'from': '10.19.17.101', 'answer':'I already added you '+data.from });
     });
 });

Server Units

 socketIo = require("socket.io"),
 ioServer = socketIo(server),
 ioClient = require("socket.io-client")('http://10.19.17.101:7700')
 ioClient.on('connect', function(){
       ioClient.on('message',function(data){
           console.log(data.from + " answered: " + data.answer);
           ioServer.to('pxe4').emit('message',data);
       });
       ioClient.emit('message',{ 'from': 'pxe4', 'ip':'10.19.16.84' });
 });

 ioServer.sockets.on("connection",function(socket){
     // Display a connected message
     console.log("User-Client Connected!");
     // When we receive a message...
     socket.on("message",function(data){
        // We got a message... I dunno what we should do with this...
        console.log(data);
        console.log(data.from + " is connected with ip " + data.ip);
        socket.emit('message', { 'from': '10.19.16.84', 'answer':'I already added you '+data.from });
        ioClient.emit("message",data);
    });
    socket.on("disconnect",function(data){
        // We need to notify Server 2 that the client has disconnected
        ioClient.emit("message","UD,"+socket.id);
        // Other logic you may or may not want
        // Your other disconnect code here
    });

 });

Units

 ioClient = require("socket.io-client")('http://10.19.16.84:7770'),
 ioClient.on('connect', function(){
      ioClient.on('message',function(data){
         // We received a message from Server 2
         // We are going to forward/broadcast that message to the "Lobby" room
         console.log(data.from + " answered: " + data.answer);
      });
      ioClient.emit('message',forsend);
 });

I was wondering if at this moment I can send some information from Main Server to a specific unit?

If someone could help me, I will be thankful.


Solution

When connecting from each client on the main server or Server Units you recive a socket object which contains socketid. You have to save those socket id's in some data storge for speedy access with the server information. When you have to emit data to specific socket you have to query that specific socket from data storage and emit the data. On disconnect you have to pull that particular socket from data storage



Answered By - Asif Saeed
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing