【问题标题】:React js Socket.io and State反应 js Socket.io 和状态
【发布时间】:2020-03-18 18:12:01
【问题描述】:

您好,我正在尝试将我的套接字通过后端发送给我的响应保存在一个状态中

这是我向套接字发送消息的方法

export default class Home extends Component { 

    constructor(){
        super()

        this.state = {
          informationReceived: 'Nothing yet!'
        }
        const token = localStorage.getItem('token');
        const socket = io('http://localhost:8080', {
            query: { token: token }
        });

        socket.on('success', (receivedInfo) => {
            this.setState({
              informationReceived: receivedInfo
            })
          })

      }

      emitInfoToAll = () => {
        const token = localStorage.getItem('token');
        console.log('entrou')
        const socket = io('http://localhost:8080',{
            query: { token: token }
        });

        socket.emit('myevent', 'Hello realtime connected users!')
        console.log(this.state.informationReceived);
      }
}

但是这样我会在发送和接收时打开到我的套接字的连接两次 我想知道如何只能打开一个连接,所以当我向后端发送回复时,我不必再次打开

我该如何设置我为我的p标签获得的这些值:

                <p> Name: <span className = "name"> </span> </p>
                <p> Points: <span className = "points"> 0 </span> </p>

【问题讨论】:

    标签: javascript node.js reactjs socket.io


    【解决方案1】:

    您应该将socket 作为组件的状态,以便您可以在emitInfoToAll 方法中模拟它

    export default class Home extends Component { 
    
    constructor(){
        super()
    
        this.state = {
          informationReceived: 'Nothing yet!'
          socket: null;
        }
    
        const token = localStorage.getItem('token');
        socket = io('http://localhost:8080', {
            query: { token: token }
        });
    
        socket.on('success', (receivedInfo) => {
            this.setState({
              informationReceived: receivedInfo
            })
          })
    
      }
    
      emitInfoToAll = () => {
        const { socket } = this.state;
        console.log('entrou')
        socket.emit('myevent', 'Hello realtime connected users!')
        console.log(this.state.informationReceived);
      }
    }
    

    更新:帮助将值设置为p 标签

    export default class Home extends Component {
    
        constructor() {
            super()
    
            this.state = {
                informationReceived: 'Nothing yet!',
                socket: null
            }
    
            const token = localStorage.getItem('token');
            socket = io('http://localhost:8080', {
                query: { token: token }
            });
    
            socket.on('success', (receivedInfo) => {
                this.setState({
                    informationReceived: receivedInfo
                })
            })
    
        }
    
        emitInfoToAll = () => {
            const { socket } = this.state;
            console.log('entrou')
            socket.emit('myevent', 'Hello realtime connected users!')
            console.log(this.state.informationReceived);
        }
    
        render() {
            const { informationReceived } = this.state;
            const { name, point } = informationReceived;
            return (
                <div>
                    <p> Name: <span className="name">{name}</span> </p>
                    <p> Points: <span className="points">{point}</span> </p>
                </div>
            );
        }
    }
    

    【讨论】:

    • @gabriel 我已经更新了帮助您将下载的值设置为 p 标记的新代码。如果有效,请检查并标记为正确。谢谢
    • 为什么不使用socket.on 来监听后端哪些数据发生了变化?
    • 最好使用useMemo 但我认为最好使用context 并将您的套接字客户端放在那里,然后您可以使用上下文或消费者在任何地方访问您的套接字客户端。
    • @VahidAlimohamadi 是的,你的建议更好。但那样的话我只是帮他尽快绕过问题
    • 现在你可以把你的socket事件放在componentDidMount如果你使用react-hooks它等于useEffect但是第二个参数是空数组。
    猜你喜欢
    • 2020-11-28
    • 2020-03-15
    • 2020-12-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多