[英]GET /socket.io/?EIO=3&transport=polling&t=NZFvwqb 404 149
I got the above exception when I try to implement socket.io
to count active users, i tried every solution but nothing works for me.当我尝试实施
socket.io
来计算活跃用户时,我遇到了上述异常,我尝试了所有解决方案,但对我没有任何效果。
Server:服务器:
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");
});
Change this:改变这个:
app.listen(8080, () => {
console.log("Server is running on port 8080");
});
To this:对此:
server.listen(8080, () => {
console.log("Server is running on port 8080");
});
You are facing this issue because the instance you've passed when initializing socket
and instance you are listening to are different.您正面临这个问题,因为您在初始化
socket
时传递的实例和您正在侦听的实例不同。
For more refer this: Should I create a route specific for SocketIO?有关更多信息,请参阅: 我应该为 SocketIO 创建一个特定的路由吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.