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
如何解决这个错误?
我安装了 express、nodemon 和 socket.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
控制台Visual Studio代码:
Visual studio 代码的控制台仅显示我刷新浏览器或 ctrl+S 代码的次数:
[nodemon] starting 'node apde app.js'
您添加了斜线而不是 semi-colon:
socket.current = io("ws://localhost:4000")
我正在建立一个论坛,两个用户在连接后可以 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
如何解决这个错误?
我安装了 express、nodemon 和 socket.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
控制台Visual Studio代码: Visual studio 代码的控制台仅显示我刷新浏览器或 ctrl+S 代码的次数:
[nodemon] starting 'node apde app.js'
您添加了斜线而不是 semi-colon:
socket.current = io("ws://localhost:4000")