![](/img/trans.png)
[英]GET http://localhost/socket.io/?EIO=3&transport=polling&t=LjIvLGU 404 (Not Found)
[英]GET /socket.io/?EIO=3&transport=polling&t=NZFvwqb 404 149
当我尝试实施socket.io
来计算活跃用户时,我遇到了上述异常,我尝试了所有解决方案,但对我没有任何效果。
服务器:
const express = require("express");
const app = express();
const cors = require("cors");
const socketIo = require('socket.io');
const http = require('http');
//Enable CORS policy
app.use(cors());
app.options("*", cors());
//socket io
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origins: ["http://localhost:4200", "http://localhost:3000"],
methods: ["GET", "POST"],
credentials: false
}
});
var count = 0;
io.on('connection', (socket) => {
console.log("Client connected");
if (socket.handshake.headers.origin === "http://localhost:3000") {
count++;
socket.broadcast.emit('count', count);
socket.on('disconnect', () => {
count--;
socket.broadcast.emit('count', count);
});
}
});
//Server
app.listen(8080, () => {
console.log("Server is running on port 8080");
});
改变这个:
app.listen(8080, () => {
console.log("Server is running on port 8080");
});
对此:
server.listen(8080, () => {
console.log("Server is running on port 8080");
});
您正面临这个问题,因为您在初始化socket
时传递的实例和您正在侦听的实例不同。
有关更多信息,请参阅: 我应该为 SocketIO 创建一个特定的路由吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.