简体   繁体   English

GET /socket.io/?EIO=3&transport=polling&t=NZFvwqb 404 149

[英]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.

相关问题 GET http://localhost/socket.io/?EIO=3&transport=polling&t=LjIvLGU 404(未找到) - GET http://localhost/socket.io/?EIO=3&transport=polling&t=LjIvLGU 404 (Not Found) GET http://localhost:8563/socket.io/?EIO=3&transport=polling&t=NEsUPis 404(未找到) - GET http://localhost:8563/socket.io/?EIO=3&transport=polling&t=NEsUPis 404 (Not Found) socket.io GET /socket.io/?EIO=3&transport=polling&t=LV9VGzC“错误(404):“未找到” - socket.io GET /socket.io/?EIO=3&transport=polling&t=LV9VGzC“ Error (404): ”Not found" GET http://MY_DOMAIN.com/socket.io?EIO=3&transport=polling&t=1449205340962-2 404(未找到) - GET http://MY_DOMAIN.com/socket.io?EIO=3&transport=polling&t=1449205340962-2 404 (Not found) 无法获取 /socket.io/?EIO=3&transport=polling&t=LdmmKYz - Cannot GET /socket.io/?EIO=3&transport=polling&t=LdmmKYz (IONIC 2)错误:socket.io集成Express,GET http:// localhost:3000 / socket.io /?EIO = 3&transport = polling&t = LvraMVg 404(未找到)(WEBRTC) - (IONIC 2) Error : socket.io integrate express, GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LvraMVg 404 (Not Found) (WEBRTC) 当我在服务器上部署时,不断收到“ socket.io/?EIO=3&transport=polling&t=Lvm1SGO”404错误 - Keep getting “socket.io/?EIO=3&transport=polling&t=Lvm1SGO” 404 errors when I deploy on server 加载资源失败:服务器响应状态为404(未找到)socket.io/?EIO=3&transport=polling&t=1503062449710-0 - Failed to load resource: the server responded with a status of 404 (Not Found) socket.io/?EIO=3&transport=polling&t=1503062449710-0 Node.js socket.io/?EIO=3&transport=polling&t=LcuVjT6 404(未找到)反向代理 - Node.js socket.io/?EIO=3&transport=polling&t=LcuVjT6 404 (Not Found) reverse proxy socket.io/?EIO=3&transport=polling&t=1345678不收费 - socket.io/?EIO=3&transport=polling&t=1345678 no charging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM