Socket.io 找不到本地主机
Socket.io can't find localhost
我一直在关注教程中的 this basic tutorial, when I quickly found out that socket.io doesn't seem to detect my index.html and localhost gives error message 404. So maybe I had mistyped something, thus I copied the files,但同样的问题仍然存在。我错过了什么?
Socket.io 本身仅响应 socket.io 请求,而不响应常规网页请求。因此,如果您希望您的服务器既能与 socket.io 一起工作,又能成为提供网页服务的常规网络服务器,那么您需要将常规网络服务器与您的 socket.io 代码集成在一起。
socket.io 网站有一个如何使用 socket.io WITH Express 的示例,例如 here。基本概念(来自 socket.io 文档)是这样的:
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log('a user connected');
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
我一直在关注教程中的 this basic tutorial, when I quickly found out that socket.io doesn't seem to detect my index.html and localhost gives error message 404. So maybe I had mistyped something, thus I copied the files,但同样的问题仍然存在。我错过了什么?
Socket.io 本身仅响应 socket.io 请求,而不响应常规网页请求。因此,如果您希望您的服务器既能与 socket.io 一起工作,又能成为提供网页服务的常规网络服务器,那么您需要将常规网络服务器与您的 socket.io 代码集成在一起。
socket.io 网站有一个如何使用 socket.io WITH Express 的示例,例如 here。基本概念(来自 socket.io 文档)是这样的:
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log('a user connected');
});
server.listen(3000, () => {
console.log('listening on *:3000');
});