socket.io - 无法加载资源:net::ERR_CONNECTION_REFUSED

socket.io - Failed to load resource: net::ERR_CONNECTION_REFUSED

我正在建立一个论坛,两个用户在连接后可以 post 状态然后对他们发表评论。 对于评论,我使用 socket.io .

在控制台中,我每隔几秒就会收到此错误:

Failed to load resource: net::ERR_CONNECTION_REFUSED
GET http://localhost/socket.io/?EIO=4&transport=polling&t=O05nump net::ERR_CONNECTION_REFUSED

如何解决这个错误?


我安装了 expressnodemonsocket.io 36=]套接字服务器:

package.json

{
"name": "socket",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
  "express": "^4.17.3",
  "nodemon": "^2.0.15",
  "socket.io": "^4.4.1"
  }
}

app.js

const io = require("socket.io")(4000, {
cors: {
    origin: "http//localhost:3000",
},
});


io.on('connection', (socket) => {
  console.log('A new user is connected !');
})
io.on('disconnect', () => {
  console.log('User has left !');
});

此外,我在客户端(前面)安装了socket.io-client,然后在我的网页中,我添加了这段代码:

import { io } from 'socket.io-client'
export default () => {

  //implement socket
  const socket = useRef()

  useEffect(() => {
      socket.current = io("ws://localhost/4000")
  }, [])

return (
  //some html code
)
}

项目论坛

├── back     
├── client
   └──index.jsx
└── socket
   └──package.json
   └──app.js

您添加了斜线而不是 semi-colon:
socket.current = io("ws://localhost:4000")