English 中文(简体)
为什么单体背书的电文仅归还其他用户信息
原标题:Why message in socketio backend return only other user message

页: 1 为了建造聊天室,但当输入信息时,只有其他用户看到电文时,作者们看不到任何东西,而其他信息如上面的形象。 这是我的法典:

const express = require("express");
const app = express();
const PORT = 4000;

//New imports
const http = require("http");
const cors = require("cors");

const { Server } = require("socket.io");

const server = http.createServer(app);

const io = new Server(server, {
  cors: {
    origin: "http://localhost:3000",
    methods: ["GET", "POST"],
  },
});

io.on("connection", (socket) => {
  console.log(`⚡: ${socket.id} user just connected!`);
  socket.on("join_room", (data) => {
    socket.join( 123 );
  });
  
  socket.on("send_message", (data) => {
    socket.to( 123 ).emit("receive_message", `${socket.id}: ${data}`);
  });
  
  socket.on("disconnect", () => {
    console.log("?: A user disconnected");
  });
});

app.use(cors());

server.listen(PORT, () => {
  console.log(`Server listening on ${PORT}`);
});

enter image description here

And front end

import React, { useState, useEffect } from  react ;
import { ConnectionState } from  ./components/ConnectionState ;
import { ConnectionManager } from  ./components/ConnectionManager ;
import { MyForm } from  ./components/MyForm ;
import { socket } from  ./socket ;
import { Events } from  ./components/Events ;
import { Button } from  @mui/material ;

export default function App() {
  const [isConnected, setIsConnected] = useState(socket.connected);
  const [fooEvents, setFooEvents] = useState<any>([]);
  const [joinRoom, setJoinRoom] = useState<{
    name: string;
    room: string;
    time: Date;
  }>();

  function onJoinRoom() {
    socket.emit( join_room ,  123 );
  }

  useEffect(() => {
    function onConnect() {
      setIsConnected(true);
    }

    function onDisconnect() {
      setIsConnected(false);
    }

    socket.on( connect , onConnect);
    socket.on( disconnect , onDisconnect);

    return () => {
      socket.off( connect , onConnect);
      socket.off( disconnect , onDisconnect);
    };
  }, []);

  useEffect(() => {
    function onFooEvent(value: any) {
      setFooEvents((previous: any) => [...previous, value]);
    }
    
    socket.on( receive_message , onFooEvent);

    return () => {
      socket.off( receive_message , onFooEvent);
    };
  }, [socket]);

  return (
    <div className="App">
      <ConnectionState isConnected={isConnected} />
      <Events events={fooEvents} />
      <Button onClick={onJoinRoom}>Join Room</Button>
      <ConnectionManager />
      <MyForm />
    </div>
  );
}

连接国是一个不付钱的连接状态,事件是,信息显示、只有阵列、联系管理人通连接的纽伦,我的表格是输入信息并提交。

问题回答

You should put message state into useEffect s dependencies.

useEffect(() => {
  function onFooEvent(value: any) {
    setFooEvents((previous: any) => [...previous, value]);
  }

  socket.on( receive_message , onFooEvent);

  return () => {
    socket.off( receive_message , onFooEvent);
  };
}, [socket, fooEvents]);




相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...

热门标签