Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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
Node.js react客户端:websocket.js:83 websocket连接到';ws://localhost:3009/socket.io/?EIO=4&;传输=websocket';失败:_Node.js_Reactjs_Sockets_Websocket_Socket.io - Fatal编程技术网

Node.js react客户端:websocket.js:83 websocket连接到';ws://localhost:3009/socket.io/?EIO=4&;传输=websocket';失败:

Node.js react客户端:websocket.js:83 websocket连接到';ws://localhost:3009/socket.io/?EIO=4&;传输=websocket';失败:,node.js,reactjs,sockets,websocket,socket.io,Node.js,Reactjs,Sockets,Websocket,Socket.io,我有一个使用socket io的节点后端 app.js中的第一个初始化te应用程序 const express = require("express") const app = express() module.exports = { app, express } 然后在io.js中,我创建了套接字服务器 const { app } = require("./app"); const http = require("http

我有一个使用socket io的节点后端

app.js中的第一个初始化te应用程序

const express = require("express")

const app = express()

module.exports = {
    app,
    express
}
然后在io.js中,我创建了套接字服务器

const { app } = require("./app");
const http = require("http");
const socketio = require("socket.io");
const server = http.createServer(app);
const io = socketio(server);

module.exports = io;
然后在server.js中,首先为api调用导入app.js,然后导入io.js

require("dotenv").config();
const { app, express } = require("./app");
const logger = require("./logger");

const io = require("./io");
然后我只需在server.js中添加emit-listen代码

io.on("connection", (socket) => {
  console.log("we have a new connection");

  socket.on("disconnect", () => {
    console.log("the socket disconnected");
  });

  socket.on("join", ({ user_id }, callback) => {
    // const notification = getListNotifications(user_id);
    // const popup = getUserPopup(user_id);
    // socket.emit("nofication", { popup: popup.count, notification });
    socket.emit("nofication", { popup: 3, notificaton: { a: 1 } });
    socket.join(user.room);
    callback();
  });
然后我在dev模式下运行server.js文件nodemon server.js

io.on("connection", (socket) => {
  console.log("we have a new connection");

  socket.on("disconnect", () => {
    console.log("the socket disconnected");
  });

  socket.on("join", ({ user_id }, callback) => {
    // const notification = getListNotifications(user_id);
    // const popup = getUserPopup(user_id);
    // socket.emit("nofication", { popup: popup.count, notification });
    socket.emit("nofication", { popup: 3, notificaton: { a: 1 } });
    socket.join(user.room);
    callback();
  });
然后在react中,我只使用socket.io

import io from "socket.io-client";

useEffect(() => {
    socket = io("ws://localhost:3009", {
      "force new connection": true,
      reconnectionAttempts: "Infinity",
      timeout: 10000,
      transports: ["websocket"],
    });

   

    return () => {
      socket.disconnect();
    };
  }, []);
它在浏览器控制台中给了我这个错误

server node.js控制台正在接收https协议

我在其他答案中发现,这可能是一些协议问题

很高兴向你学习。提前谢谢