Home > Blockchain >  Why the module C does not receive the socket io "hi" event?
Why the module C does not receive the socket io "hi" event?

Time:11-26

How to call different module for different socket path? Here is server code:

require('dotenv-flow').config();
const express = require('express');
const app = express();
const http =require('http');
const httpServer= http.createServer(app);
const io = require('socket.io')(httpServer);

let C=require('./C');
let c=new C(io.of("/c"));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

class C {
    constructor(socket) {
        socket.on("connection",()=>{
            console.log("Connection to /c");
        });
        socket.on("hi", (peerName, calllBack) => {
            console.log("Received say hi from "   peerName   ".");
        });
    }
}
module.exports = C;
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

The client side code:

import { useEffect} from "react";
import io from "socket.io-client";
export default function TestSocket() {
    useEffect(() => {
        let peerName;
        let signalServerURL=process.env.REACT_APP_SOCKET_URL   "c";
        let sUsrAg = navigator.userAgent;
        if (sUsrAg.indexOf("Edg") > -1) {
            peerName = "Edge";
        } else {
            if (sUsrAg.indexOf("Chrome") > -1) {
                peerName = "Chrome";
            } else {
                if (sUsrAg.indexOf("Firefox") > -1) {
                    peerName = "Firefox";
                } else {
                    if (sUsrAg.indexOf("Safari") > -1) {
                        peerName = "Safari";
                    }
                }
            }
        }
        let socket = io(signalServerURL, {
            transports: ["websocket"],
        });
        socket.emit("hi", peerName, (response) => {
            console.log(response);
        });
    },[]);
   return (
        <></>
    )    
}
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

The module C can receive "connection" event, however, it cannot receive the "hi" event, why? Besides that, I want to use different module/class to handle different socket path.

e.g.

for the path /b socket access, I want to use the module B to handle it.

for the path /d socket access, I want to use the module D to handle it.

How can I implement it?

According to the Philippe answer, I modified the server code as below:

require('dotenv-flow').config();
const express = require('express');
const app = express();
const http =require('http');
const httpServer= http.createServer(app);
const io = require('socket.io')(httpServer);

let C=require('./C');
let c=new C(io,"/c");
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

class C {
    constructor(io,path) {
         io.of(path).on("connection",socket=>{
           console.log("Connection to " path);
           
           socket.on("hi", (peerName, calllBack) => {
            console.log("Received say hi from "   peerName   ".");
           });
         });
    }
}
module.exports = C;
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

It works!

CodePudding user response:

When a connection is successful, socket-io returns the socket you need to use. You can try something like:

constructor(io) {
    io.on("connection", (socket) => {
        console.log("Connection to /c "   socket.id);
        socket.on("hi", (peerName, calllBack) => {
            console.log("Received say hi from "   peerName   ".");
        });
    });
}
  • Related