简体   繁体   English

反应组件socket.io不必要的连接

[英]React component socket.io unnecessary connection

I'm using socket.io-client in my react component. 我在我的react组件中使用socket.io-client。 This is how most of the tutorials sugest to do that: 这是大多数教程建议这样做的方式:

import openSocket from 'socket.io-client';
const socket = openSocket('http://localhost:8000');

This works fine (I was able to use socket.on and socket.emit in my functions/lifecycle hooks). 这个工作正常(我能够在我的功能/生命周期挂钩中使用socket.on和socket.emit)。

But declaring openSocket in this way causes connection request (every 25 second) even if component didn't mounted. 但是以这种方式声明openSocket会导致连接请求(每25秒),即使未安装组件也是如此。 This gives me an error (when I use npm start without backend) ERR_CONNECTION_REFUSED in console. 这给我一个错误(当我使用不带后端的npm start )控制台中的ERR_CONNECTION_REFUSED

I found half-way solution for that by doing 我通过这样做找到了中途解决方案

componentWillUnmount(){
  socket.disconnect();
}

The problem with this is that it works only when I mount and after that unmount the component, the connection is still running if I just upen another rout like homepage for example. 问题是,仅当我挂载该组件时,该组件才有效,然后卸载该组件,如果我只是抬起另一个主页(例如主页),则连接仍在运行。

Qustion: What is the best way to handle this? 问题:处理此问题的最佳方法是什么? Where should I declare 我应该在哪里申报

const socket = openSocket('http://localhost:8000');

Thanks, and sorry for my Eng. 谢谢,对不起,我的英语。

I think you'd prefer to create the connection when you actually need it. 我认为您希望在实际需要时创建连接。 So, maybe when the component will or did mount. 因此,也许什么时候该组件将被挂载。 And dispose it right after you don't need it anymore. 并在不再需要它后立即进行处理。

Which component? 哪个成分? I think would be the most top level component that depends on that connection. 我认为这将是取决于该连接的最顶层组件。

Maybe give this a read and see if it makes sense for you to connect from a container component 也许给这个读,看看它是有道理的,你从一个容器组件连接

When you do 当你做

import io from 'socket.io-client';
const socket = openSocket('http://localhost:8000');

The code runs when the component is loaded, which is usually means the page is loaded. 该代码在加载组件时运行,这通常意味着页面已加载。 You should only open the socket when the main component's (or relevant component)'s has mounted, using componentDidMount() , and then disconnect/close when the component unmounts, using componentWillUnmount() . 仅应在安装主要组件(或相关组件)后使用componentDidMount()打开套接字,然后在卸载componentWillUnmount()时使用componentDidMount()断开连接/关闭。

Secondly, the default import shouldn't be called openSocket . 其次,默认导入不应称为openSocket The default import is a function which let's you create a socket connection but does not open it. 默认导入是一个函数,可以让您创建一个套接字连接,但不打开它。 Opening the connection is a separate call after you create the socket. 创建套接字后,打开连接是一个单独的调用。 You should call it this instead: 您应该这样称呼它:

import io from 'socket.io-client' 从'socket.io-client'导入io

Summarizing the above, you could try to do something like this: 总结以上内容,您可以尝试执行以下操作:

import io from 'socket.io-client'; // The default import should be called io.

class App extends React.Component {
  constructor(props) {
    super(props);
    this.socket = io('http://localhost:8000');
  }

  componentDidMount() {
    this.socket.open();
    this.socket.emit('load_settings');
    this.socket.on('settings_loaded', (data) => {
      // we get settings data and can do something with it
      this.setState({
        settings: data,
      })
    });
  }

  componentWillUnmount() {
    this.socket.close();
  }

  render() {
    ...
  }
}

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

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