程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了GET /socket.io/?EIO=3&transport=polling&t=NZFvwqb 404 149大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

开发过程中遇到GET /socket.io/?EIO=3&transport=polling&t=NZFvwqb 404 149的问题如何解决?下面主要结合日常开发的经验,给出你关于GET /socket.io/?EIO=3&transport=polling&t=NZFvwqb 404 149的解决方法建议,希望对你解决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");
});

解决方法

改变这个:

[1] pry(main)> require 'chef/node'
=> true
[2] pry(main)> node = Chef::Node.new
[....]
[3] pry(main)> node.default["fizz"]["buzz"] = { "foo" => [ { "bar" => "baz" } ] }
=> {"foo"=>[{"bar"=>"baz"}]}
[4] pry(main)> buzz = node["fizz"]["buzz"].to_hash
=> {"foo"=>[{"bar"=>"baz"}]}
[5] pry(main)> buzz.class
=> Hash
[6] pry(main)> buzz["foo"].class
=> Array
[7] pry(main)> buzz["foo"][0].class
=> Hash
[8] pry(main)>

为此:

app.listen(8080,() => {
  console.log("Server is running on port 8080");
});

您遇到此问题是因为您在初始化 server.listen(8080,() => { console.log("Server is running on port 8080"); }); 时传递的实例与您正在侦听的实例不同。

有关更多信息,请参阅: Should I create a route specific for SocketIO?

大佬总结

以上是大佬教程为你收集整理的GET /socket.io/?EIO=3&transport=polling&t=NZFvwqb 404 149全部内容,希望文章能够帮你解决GET /socket.io/?EIO=3&transport=polling&t=NZFvwqb 404 149所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:149404