简体   繁体   English

Socket.on 没有触发

[英]Socket.on is not firing

I am building a message board as a learning exercise and have working notifications and a chat app working with socket.io.我正在构建一个留言板作为学习练习,并且有工作通知和一个使用 socket.io 的聊天应用程序。

I am trying to integrate a basic video call functionality, but I am stumped on some basic preparations for that feature.我正在尝试集成基本的视频通话功能,但我很难为该功能做一些基本的准备工作。 My socket.on code isn't firing when the server emits the relevant function and I have no clue why.当服务器发出相关函数时,我的 socket.on 代码没有触发,我不知道为什么。 The console logs around the emits are all executing, so I know the code is being reached.发出的控制台日志都在执行,所以我知道正在访问代码。

On the client my socket.on code is in the same component as my notifications and they are working and it is definitely mounted.在客户端上,我的 socket.on 代码与我的通知在同一个组件中,并且它们正在工作并且它肯定已安装。

It's the userOff and receiveCall functions that aren't being executed (for whatever reason)...这是没有被执行的 userOff 和 receiveCall 函数(无论出于何种原因)......

Any help will be appreciated.任何帮助将不胜感激。

Server:服务器:

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('userOff');
      console.log('offline emitted');
    }
  });
  socket.on('disconnect', () => {
    socket.disconnect(true);
  });
});

client:客户:

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('userOff', () => {
      console.log('user offline');
    });
    socket.on('recieveCall', (id) => {
      console.log('recieve call');
    });
    socket.on('mailNotification', () => {
      document.getElementById('notifyMail').classList.add('notify');
    });
    socket.on('boardsNotification', () => {
      document.getElementById('notifyBoards').classList.add('notify');
    });
  }, []);

  return null;
};

export default Socket;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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