Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的客户没有听到套接字io事件?_Javascript_Node.js_Reactjs_Websocket_Socket.io - Fatal编程技术网

Javascript 为什么我的客户没有听到套接字io事件?

Javascript 为什么我的客户没有听到套接字io事件?,javascript,node.js,reactjs,websocket,socket.io,Javascript,Node.js,Reactjs,Websocket,Socket.io,我不明白为什么我的事件处理程序没有为userOffline事件触发。我的其他活动进展顺利 下面我将发布我的代码 这是我的服务器: io.on('connection', (socket) => { socket.emit('messageFromServer'); socket.on('messageToServer', (dataFromClient) => { connectedUsers[dataFromClient.username] = socket;

我不明白为什么我的事件处理程序没有为userOffline事件触发。我的其他活动进展顺利

下面我将发布我的代码

这是我的服务器:

io.on('connection', (socket) => {
  socket.emit('messageFromServer');
  socket.on('messageToServer', (dataFromClient) => {
    connectedUsers[dataFromClient.username] = socket;
  });
  socket.on('join', ({ username, room }) => {
    socket.join(room);
    socket.emit('message', 'Welcome!');
    socket.broadcast
      .to(room)
      .emit('message', `${username} has joined the room!`);
  });
  socket.on('messageRoom', ({ username, room, message }) => {
    socket.broadcast.to(room).emit('message', `${username}: ${message}`);
  });
  socket.on('call', ({ username, id }) => {
    if (connectedUsers[username]) {
      connectedUsers[username].emit('recieveCall', id);
      console.log('online emitted');
    } else {
      socket.emit('userOffline');
      console.log('offline emitted');
    }
  });
  socket.on('disconnect', () => {
    socket.disconnect(true);
  });
});
客户:

import React, { useContext, useEffect } from 'react';
import socketIOClient from 'socket.io-client';
import StateContext from '../StateContext';
import DispatchContext from '../DispatchContext';
const endpoint = 'http://localhost:5000';

const Socket = () => {
  const appState = useContext(StateContext);
  const appDispatch = useContext(DispatchContext);
  const socket = socketIOClient(endpoint);

  useEffect(() => {
    socket.on('messageFromServer', () => {
      socket.emit('messageToServer', { username: appState.username });
    });
    socket.on('mailNotification', () => {
      document.getElementById('notifyMail').classList.add('notify');
    });
    socket.on('boardsNotification', () => {
      document.getElementById('notifyBoards').classList.add('notify');
    });
    socket.on('userOffline', () => {
      console.log('user offline');
      appDispatch({
        type: 'flashMessage',
        value: 'User is offline',
        fmType: 'danger',
      });
    });
  }, []);

  return null;
};

export default Socket;
注意:调用事件由不同的react组件触发


知识是一种美德,是一种美德,是一种美德,是一种美德。但是,在最低限度上,我们需要一个实验室来进行日常工作。两人或两人在一个无教区的房间里互相指责。除非是出于非故意的原因,否则不能将mollit anim id视为劳动的罪过。

您可以设置
connectedUsers[dataFromClient.username]=socketif(connectedUsers[username]){
始终为true,并且代码
socket.emit('userOffline');
永远无法访问它,直到它们联机后才为null。我正在运行console.log,但没有事件处理程序,也不知道为什么。。。