错误:http://localhost:3001/socket.io/404(未找到) [英] Error: http://localhost:3001/socket.io/ 404 (Not Found)
问题描述
我正在尝试使用socket.io连接客户端-服务器,但是在测试我的代码时,出现此错误
I am trying to connect client-server using socket.io however at the moment of testing my code I am getting this error http://localhost:3001/socket.io/?EIO=3&transport=polling&t=MgUqscg 404 (Not Found)
我已经尝试了两种不同的方法来实现这一目标.一个返回已指定的错误,第二个返回错误监听EADDRINUSE ::: 3001
有人可以向我解释我做错了什么吗?所有教程看起来都很简单,但是我现在还停留在这一点,我真的想学习如何使用套接字来提高自己的技能.
I already tried two different ways to achieve this. One returns the error already specified the second one returns an error listen EADDRINUSE :::3001
can somebody please explain to me what am I doing wrong? all tutorials seem so easy but I am stuck at this point and I really want to learn how to use sockets to improve my skills.
这是我用于服务器
const express = require('express');
const http = require('http');
const app = express();
// Web socket config option #1
var server = http.Server(app);
var io = require('socket.io')(server);
// Web socket config option #2
var io = require('socket.io').listen(server);
server.listen(PORT)
const PORT = process.env.PORT || 3001;
app.set('port', 3000)
io.on('connection', (socket) =>{
console.log('a user is connected')
})
app.listen(PORT, function(){
console.log(' ======= SERVER RUNNING =======');
})
这是我给客户
import * as io from 'socket.io-client';
private socket = io(`http://localhost:3001`)
我只想成功连接客户端-服务器而不会出现错误,并打印以控制台显示成功消息.预先感谢.
I just want to connect client-server successfully without errors and print to console a success message. Thanks in advance.
推荐答案
我通过使用其他端口发送secket.io事件解决了该问题. server.listen(4444)
I solved it by using a different port for sending secket.io events.
server.listen(4444)
这篇关于错误:http://localhost:3001/socket.io/404(未找到)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!