socket.io-无法加载资源:net :: err_connection_refuse
我正在建立一个论坛,连接后的两个用户可以发布状态然后对他们发表评论。 对于评论,我使用了 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:
- 套接字服务器:
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 !');
});
- Front:
另外,我在客户端安装了 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 Code 的控制台仅显示我刷新浏览器或 ctrl+S 代码的次数:
[nodemon] 启动“node apde app.js”
I'm building a forum where two users after connections can post a status then comment on them.
For comments, i used socket.io .
In console i'm getting this error each few seconds :
Failed to load resource: net::ERR_CONNECTION_REFUSED
GET http://localhost/socket.io/?EIO=4&transport=polling&t=O05nump net::ERR_CONNECTION_REFUSED
How to fix this error ?
I installed express, nodemon and socket.io in my socket server:
- Socket server:
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 !');
});
- Front:
Also, i installed socket.io-client in client (front), then in my web page, i added this code :
import { io } from 'socket.io-client'
export default () => {
//implement socket
const socket = useRef()
useEffect(() => {
socket.current = io("ws://localhost/4000")
}, [])
return (
//some html code
)
}
- My project tree:
Project Forum
├── back
├── client
└──index.jsx
└── socket
└──package.json
└──app.js
Console Visual Studio Code:
Visual studio code's console shows this only how many times i refresh browser or ctrl+S the code:[nodemon] starting 'node apde app.js'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您添加了一个斜线,而不是半彩色:
socket.current = io(“ ws:// localhost:4000”)
You added a slash instead of semi-colon:
socket.current = io("ws://localhost:4000")