我想构建socket.io服务器,但在设置客户端时出现错误(在客户端)Failed to load resource:http://localhost:3000/socket.io/?EIO=4&transport=polling&t=OK-egu3 the server responded with a status of 404 (Not Found)
验证码:
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http, {cors: {origin: "*"}});
const path = require('path');
app.set('view engine', 'ejs');
app.use(express.json({extended: true, limit: '1mb'}));
io.on('connection', (socket) => {
console.log("new connection " + socket);
});
app.get("/", (req, res, next) => {
res.render('index'/*, { ioS : io }*/);
});
console.log("Ready");
app.listen(3000);
index.ejs:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>socket Respons</h1>
</body>
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js" integrity="sha384-/KNQL8Nu5gCHLqwqfQjA689Hhoqgi2S84SNUxC3roTe4EhJ9AfLkp8QiQcU8AMzI" crossorigin="anonymous"></script>
<script>
//const io = ;
const socket = io();
console.log(socket);
</script>
</html>
我已经安装了以下库:
socket.io . io-客户端表达ejs
1条答案
按热度按时间snvhrwxg1#
您没有正确初始化客户端,您的服务器位于localhost:3000,但客户端不知道这一点,因为您没有告诉它在哪里查找。
查看socketio文档,您需要传递服务器套接字端点的URL:
值得一提的是,错误消息指出:这不是一个冗长的错误消息,但却是一个有价值的调试信号。