简体   繁体   中英

Socket.IO connecting multiple times with the server

I'm building an app for my house automation and found a problem with Socket.IO. It stays connected on the server side, but on client side I cannot get any event:

expo version: 4.5.2
react-native-cli: 2.0.1
react-native: 0.63.4
node: v12.18.2
socket.io-client: 4.1.2

Client side code:

import { io } from "socket.io-client";
const socket = io("http://my_local_ip:1337");
socket.on("connection", (data) => {
  console.log("Connected");
});
socket.on("response", (data) => console.log(data));

Server:

const express = require("express");
const app = express();
const server = require("http").createServer(app);
const io = require("socket.io")(server);

server.listen(process.env.PORT, () => {
  console.log(`Listening on  Port: ${server.address().port} ... `);
});

io.on("connection", (socket) => {
  console.log("Connected: ", socket.id);
  socket.emit("response", "hello");
}

Server output:

Listening on  Port: 1337 ... 
Connected:  cH2smrT5EaFQrx6cAAAA
Connected:  hZAUEmGjCATQapC6AAAB
Connected:  i4jFNU_UFZnlQjjQAAAC
Connected:  eGma1MSRzmpfzlXrAAAD
Connected:  tz1UDL9ASJv5WdwgAAAE
Connected:  ygfDAyyMNjdQ-LSBAAAF
Connected:  FyQN9ZfqFaplbgOwAAAG
Connected:  PF8XNqjaG6GFkMQMAAAH
Connected:  aZ0q-uCoNxQbyYM4AAAI
Connected:  YaP7MFwFAcDjj54iAAAJ
Connected:  aYTCFJeMR2bSFvfyAAAK
Connected:  750GTXaUfs41RoJ-AAAL
Connected:  cD8T5fNkQZXk5_0SAAAM
Connected:  qYo8JlpPYltCB16YAAAN
Connected:  0nUW64Zi9lzsWIDiAAAO

Nothing on client-side output

Your client side events are inappropriate, socket.io-client has following events:

socket.on("connect", () => {
    console.log("connected");
});

socket.on("data", (res) => {
    console.log(data);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM