Node.js 加载资源失败:net::ERR\u CONNECTION\u拒绝socket.io
我正在使用Socket.io、Node.js和React.js构建一个简单的聊天应用程序 后端上的服务器首先连接良好 然而,当试图从我的前端向服务器发出请求时,服务器不断崩溃。有人知道为什么吗? 这是错误Node.js 加载资源失败:net::ERR\u CONNECTION\u拒绝socket.io,node.js,reactjs,websocket,socket.io,chat,Node.js,Reactjs,Websocket,Socket.io,Chat,我正在使用Socket.io、Node.js和React.js构建一个简单的聊天应用程序 后端上的服务器首先连接良好 然而,当试图从我的前端向服务器发出请求时,服务器不断崩溃。有人知道为什么吗? 这是错误 [nodemon] 2.0.4 [nodemon] to restart at any time, enter `rs` [nodemon] watching path(s): *.* [nodemon] watching extensions: js,mjs,json [nodemon] s
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
server started on port 4000
new connection
events.js:109
throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received undefined
at checkListener (events.js:109:11)
at _addListener (events.js:344:3)
at Socket.addListener (events.js:402:10)
at Namespace.<anonymous> (/Users/macbook/Projects/chat-react/server/index.js:16:10)
at Namespace.emit (events.js:311:20)
at Namespace.emit (/Users/macbook/Projects/chat-react/server/node_modules/socket.io/lib/namespace.js:213:10)
at /Users/macbook/Projects/chat-react/server/node_modules/socket.io/lib/namespace.js:181:14
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
code: 'ERR_INVALID_ARG_TYPE'
}
[nodemon] app crashed - waiting for file changes before starting...
我的客户看起来像这样
Chat.js
import React,{useState,useffect}来自“React”;
从“查询字符串”导入quertString;
从“socket.io客户端”导入io;
让插座;
常量聊天=({location})=>{
const[name,setUserName]=useState(“”);
const[room,setRoomID]=使用状态(“”);
const ENDPOINT='localhost:4000';
useffect(()=>{
const{name,room}=querystring.parse(location.search);
套接字=io(端点);
设置用户名(名称);
setRoomID(房间);
控制台日志(套接字)
});
返回(
聊天
)
};
导出默认聊天;
任何帮助都很好。您的代码有错误。看这里:
socket.on('disconnect'),()=>{
log('用户已离开!!');
}
注意到什么了吗?是的,您想要调用的函数在括号中是而不是。尽管这不会导致语法错误,但您可能不想这样做。相反,您需要将函数放在括号内,如下所示:
socket.on('disconnect',()=>{
log('用户已离开!!');
});
注意:要了解有关逗号运算符的更多信息,请参阅以下网站:
..
const express = require ('express');
const socketio = require('socket.io');
const http = require('http');
const PORT = process.env.PORT || 4000;
const router = require('./router');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
io.on('connection', (socket) => {
console.log('new connection');
socket.on('disconnect'), () => {
console.log('User has left!!');
}
});
app.use(router);
server.listen(PORT, () => console.log(`server started on port ${PORT}`));
import React, { useState, useEffect } from 'react';
import quertString from 'query-string';
import io from 'socket.io-client';
let socket;
const Chat = ({ location }) => {
const [name, setUserName] = useState('');
const [room, setRoomID] = useState('');
const ENDPOINT = 'localhost:4000';
useEffect(() => {
const { name, room } = quertString.parse(location.search);
socket = io(ENDPOINT);
setUserName(name);
setRoomID(room);
console.log(socket)
});
return (
<h1>Chat</h1>
)
};
export default Chat;