Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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/37.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 反应客户端套接字未与服务器套接字连接_Javascript_Node.js_Reactjs_Express_Socket.io - Fatal编程技术网

Javascript 反应客户端套接字未与服务器套接字连接

Javascript 反应客户端套接字未与服务器套接字连接,javascript,node.js,reactjs,express,socket.io,Javascript,Node.js,Reactjs,Express,Socket.io,因此,我是新的套接字io和反应,我使用express作为后端。socket.io-client im使用的版本为3.0.3。im在后端使用的socket io包版本也是3.0.3,服务器通过原始html套接字连接到其他请求,但不连接react socket.io-client 客户端: import './App.css'; import {MainComponent} from "./Components/MainComponent" import {BrowserRout

因此,我是新的套接字io和反应,我使用express作为后端。socket.io-client im使用的版本为3.0.3。im在后端使用的socket io包版本也是3.0.3,服务器通过原始html套接字连接到其他请求,但不连接react socket.io-client

客户端:

import './App.css';
import {MainComponent} from "./Components/MainComponent"
import {BrowserRouter as Router} from "react-router-dom";
import openSocket from "socket.io-client";
const socket = openSocket("http://localhost:5000");


function App() {
  
  socket.on("chat", message => {
    console.log(message);
  });

  return (
    <div className="App">
    <Router>
    <MainComponent></MainComponent>
    </Router>
    </div>
  );
}

export default App;

将客户端中的第5行更改为以下内容:

const socket = openSocket("http://localhost:5000", {transports: ['websocket']});
在服务器端,更改位于第行下方

io.emit("chat","hellow world");
对此

socket.emit("chat","hellow world");
socket.emit("chat","hellow world");