简体   繁体   English

Socket.io 服务器发射仅在服务器重启后有效

[英]Socket.io Server Emit only works after server restart

Please forgive me if this comes out to be aa real stupid question.如果这是一个真正愚蠢的问题,请原谅我。
I was watching a tutorial on Socket.io and everything worked great.我正在观看有关 Socket.io 的教程,一切都很好。 So I wanted to try it in a reactjs app.所以我想在 reactjs 应用程序中尝试一下。 But the problem is that while I am able to emit from client side, I'm not able to do it from server side.但问题是,虽然我能够从客户端发出,但我无法从服务器端发出。
The emit works if I restart the server and what I mean by that is that when I run the application, initially server logs the message and after the restart, the client logs the message.如果我重新启动服务器,则发出工作,我的意思是,当我运行应用程序时,最初服务器会记录消息,重新启动后,客户端会记录消息。

client side客户端

import React, { Component } from 'react';
import socket from '../socketConfig'

class App extends Component {
    componentDidMount(){
        socket.on('hello',(data)=>{
            console.log(data);
        })
        socket.emit('call','this is from client');
    }
    render(){
        return (
            <div></div>
        )
    }
}
export default App;

socketConfig套接字配置

import openSocket from 'socket.io-client';

const socket = openSocket("http://localhost:4000/");

export default socket;

Server side服务器端

var express = require('express');

var socket = require('socket.io');
var app = express();
var server = app.listen(4000, function () {
  console.log('listening for requests on port 4000,');
});

app.use(express.static('public'));

var io = socket(server);

io.sockets.on('connection', (socket) => {
  socket.emit('hello','msg from server');
  socket.on('call', (data)=>{console.log(data)});
}

I think the issue might be in the url you are using - socket.io can be finicky since it isn't strictly a web socket, it's a wrapper around long polling and websockets.我认为问题可能出在您正在使用的 url 中 - socket.io 可能很挑剔,因为它不是严格意义上的 web 套接字,它是一个长轮询 webwrapper 和 webwrapper。 So you may have to specify which transport to use, since the socket handling is done on the same port as the http handling.因此,您可能必须指定要使用的传输,因为套接字处理是在与 http 处理相同的端口上完成的。

Try this url from the client side:从客户端试试这个 url:

ws://localhost:4000/socket.io/?EIO=3&transport=websocket

So the problem was that I was listening to events in copmponentDidMount() but using componentDidUpdate() solved the problem.所以问题是我正在听 compponentDidMount() 中的事件,但使用 componentDidUpdate() 解决了这个问题。

App Component应用组件

    state = {
        socket:null
    }
    componentDidMount(){
        this.setState({
            socket:io("http://localhost')
        })  

    }
    componentDidUpdate(){
        let sokt = this.state.socket;
        sokt.on('connect',()=>{
            sokt.on('hello',(data)=>console.log(data)); //logged: msg from server
            sokt.emit('call',"This is from client"); //logged: This is from client
        })
    }

But I'm not sure if this is correct way to do this但我不确定这是否是正确的方法

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

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