+-
当我在node.js中运行socket.io,在react.js中运行socket.io-client时,出现了无限循环。

这是我在Reactjs中写的一个组件,当我运行它时,它每秒钟都会给我以下错误:polling-xhr.js:268 GET。http:/hlocalhost:4002socket.io?EIO=3&transport=polling&t=N7zlNbY。 net::ERR_NAME_NOT_RESOLVED

import React, {useState,useEffect} from 'react';
import queryString from 'query-string';
import io from "socket.io-client";

let socket;

const Chat = ({location}) => {
    const [name, setName]=useState('');
    const [room,setRoom]=useState('');
    const ENDPOINT='hlocalhost:4002';

    useEffect(()=>{
        const {name,room}=queryString.parse(window.location.search);
        socket=io(ENDPOINT);

        setName(name);
        setRoom(room);

        console.log(socket);
    },[ENDPOINT, window.location.search]);
    return(
        <h1>Chat</h1>
        )
}

export default Chat;

部分node.js代码

const http=require('http');
const express = require('express');
const bodyParser=require('body-parser');
const bcrypt=require('bcrypt-nodejs');
const socketio=require('socket.io');
const cors=require('cors');
const PORT =process.env.PORT || 4002;

const router=require('./router');

const app=express();
const server=http.createServer(app);
const io=socketio(server);

io.on('connect',(socket)=>{
    console.log('We have a new connection!!!');
    socket.on('disconnect',()=>{
        console.log('User had left!!!');
    })
});

app.use(cors());

0
投票

你好像有一个错别字

const ENDPOINT='http://localhost:4002';