简体   繁体   English

WebSocket 连接到 'ws://localhost:5000/socket.io/?EIO=3&transport=websocket' 失败:

[英]WebSocket connection to 'ws://localhost:5000/socket.io/?EIO=3&transport=websocket' failed:

I am building a real-time chat application using react js, node, and socket.io.我正在使用 react js、node 和 socket.io 构建一个实时聊天应用程序。 But I am getting the error that the WebSocket connection has failed.但我收到 WebSocket 连接失败的错误。

I'm trying to console log the name of the room I enter and submit it should be on the console.我正在尝试控制台记录我输入的房间的名称并提交它应该在控制台上。 But it's showing this error: WebSocket connection to 'ws://localhost:5000/socket.io/?EIO=3&transport=websocket' failed:但它显示此错误: WebSocket 连接到 'ws://localhost:5000/socket.io/?EIO=3&transport=websocket' 失败:

My server is up and running.我的服务器已启动并正在运行。

Here is my client-side .这是我的客户端

import {UserContext} from '../../UserContext';
import {Link} from 'react-router-dom';
import RoomList from './RoomList';
import io from 'socket.io-client';

let socket;

const Home = () => {
    const ENDPT = 'localhost:5000';
    useEffect(() => {
        socket = io(ENDPT ,{
            cors: {
            origin: "http://localhost:5000",
            credentials: true
          },transports : ['websocket'] });
        return () => {
            socket.emit('disconnect');
            socket.off();
        }
    }, [ENDPT]);
    const { user,setUser } = useContext(UserContext);
    const [room , setRoom] = useState('');
    const handleSubmit = (e) => {
        e.preventDefault();
        socket.emit('creat-room' , room);
        console.log(room);
        setRoom('');
    }
    const rooms = [
        {
            name:"room1",
            _id:"123"
        },
        {
            name:"room2",
            _id:"456"
        }
    ]
    const setAsJohn = () => {
        const john = {
            name : "john",
            email : "john@emai.com",
            password: "123",
            id: "123",
        }
        setUser(john);
    }
    const setAsTom = () => {
        const tom = {
            name : "tom",
            email : "tom@emai.com",
            password: "456",
            id: "456",
        }
        setUser(tom);
    }
    return (
        <div>

            <div className="row">
                <div className="col s12 m6">
                <div className="card blue-grey darken-1">
                    <div className="card-content white-text">
                    <span className="card-title">Welcome {user ? user.name : ""}</span>
                    
                    <form onSubmit={handleSubmit}>
                        
                        <div className="row">
                            <div className="input-field col s12">
                            <input id="room" type="text" className="validate" placeholder='Enter room name' value={room} onChange={e => setRoom(e.target.value)}/>
                            <label htmlFor="room">Room</label>
                            </div>
                        </div>
                        <div className='btn'>Creat Room</div>
                        </form>


                    </div>
                    <div className="card-action">
                        <a href='#' onClick={setAsJohn}>John</a>
                        <a href='#' onClick={setAsTom}>Tom</a>
                    </div>
                </div>
                </div>

                <div className='col s6 m5 offset-1'>
                    <RoomList rooms={rooms}/>
                </div>
            </div>

            

            <Link to={"/chat"}>
                <button>Go to Chat</button>
            </Link>
        </div>
    )
}

export default Home

Here is my server-side code这是我的服务器端代码

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const socketio = require('socket.io');
const io = socketio(http);
const PORT = process.env.PORT || 5000;

app.get("/" , (req,res) => {
    res.send("Helloji")
})

io.on('connection', (socket) => {
    console.log('a user connected');
    socket.on('creat-room' , name =>{
        console.log('Then room name received is ' , name);  
    })
});


server.listen(PORT, () => {
    console.log(`listening on http://localhost:${PORT}`);
});```

There is something with your backend try to use ws package while using express and also Read this article How to Set Up a Websocket Server with Node.js and Express您的后端有一些东西尝试在使用 express 时使用ws package 并阅读这篇文章如何使用 Node.js 和 Express 设置 Websocket 服务器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:websocket.js:84 WebSocket 连接到“ws://localhost:5000/socket.io/?EIO=4&transport=websocket”失败: - error: websocket.js:84 WebSocket connection to 'ws://localhost:5000/socket.io/?EIO=4&transport=websocket' failed: GET http://localhost:8563/socket.io/?EIO=3&transport=polling&t=NEsUPis 404(未找到) - GET http://localhost:8563/socket.io/?EIO=3&transport=polling&t=NEsUPis 404 (Not Found) 在 ejs 和 nodejs 中发布 http://localhost:3000/socket.io/?EIO=3&amp;transport=polling&amp;t=NQUneY3 400(错误请求) - POST http://localhost:3000/socket.io/?EIO=3&transport=polling&t=NQUneY3 400 (Bad Request) in ejs and nodejs GET http://localhost:3306/socket.io/?EIO=3&amp;transport=polling&amp;t=NQfLxAy net::ERR_INVALID_HTTP_RESPONSE - GET http://localhost:3306/socket.io/?EIO=3&transport=polling&t=NQfLxAy net::ERR_INVALID_HTTP_RESPONSE 无法连接socket.io节点服务器以响应前端。 “ POST http:// localhost:3000 / socket.io /?EIO = 3&transport = polling&t = MoHNIJT 404(未找到)” - Can't connect socket.io node server to react frontend. “POST http://localhost:3000/socket.io/?EIO=3&transport=polling&t=MoHNIJT 404 (Not Found)” 在Socket.IO和WS之间共享WebSocket连接 - Sharing a WebSocket connection between Socket.IO and WS Phaser.js中的Node.js和Socket.io未连接socket.io/?EIO=3&amp;transport=polling - Node.js and Socket.io in Phaser.js not connecting socket.io/?EIO=3&transport=polling phoenix.js:与ws:// localhost:4000 / socket的WebSocket连接失败 - phoenix.js: WebSocket connection to ws://localhost:4000/socket failed GET http://MY_DOMAIN.com/socket.io?EIO=3&amp;transport=polling&amp;t=1449205340962-2 404(未找到) - GET http://MY_DOMAIN.com/socket.io?EIO=3&transport=polling&t=1449205340962-2 404 (Not found) websocket连接无效-Socket.IO - websocket connection invalid - Socket.IO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM