【问题标题】:Why is my Socket.IO server not responding to my Firecamp and C# clients?为什么我的 Socket.IO 服务器没有响应我的 Firecamp 和 C# 客户端?
【发布时间】:2021-05-18 22:40:41
【问题描述】:

我正在尝试设置一个非常基本的 Socket.IO 服务器和一个 .NET / Firecamp 客户端来学习如何在两者之间发送事件。

我的 Javascript Socket.IO 服务器是这样设置的:

const
    http = require("http"),
    express = require("express"),
    socketio = require("socket.io");    

const app = express();
const server = http.createServer(app);    
const io = socketio(server);
const SERVER_PORT = 3000;

io.on("connection", () => {
    console.log("Connected");
    io.emit("foo", "123abc");
});

server.listen(SERVER_PORT);

我可以连接一个简单的 Socket.IO Javascript 文件:

const
    io = require("socket.io-client"),
    ioClient = io.connect("http://localhost:3000");

ioClient.on('connect', () => {
    console.log("connected");    
});

当我尝试连接 Firecamp 或 this C# 库时,我从未看到触发连接事件。

我查看了 Socket.IO JS 客户端的默认选项并尝试在 Firecamp 中重现它们:https://socket.io/docs/v3/client-api/index.html

最重要的似乎是Path= /socket.ioForceNew = TrueTransports = polling, websocket。我决定删除轮询传输,因为我不断收到 XHR 轮询错误,但 websocket 在 C# 和 Firecamp 中也超时。

我尝试连接到“http://localhost:3000”和“http://localhost”。 这是screenshot of my Firecamp settings

我的 C# 程序也有类似的问题

  Quobject.Collections.Immutable.ImmutableList<string> trans = Quobject.Collections.Immutable.ImmutableList.Create<string>("websocket");
            IO.Options options = new IO.Options();
            options.Port = 3000;
            options.Agent = false;
            options.Upgrade = false;
            options.Transports = trans;
            
            client = IO.Socket("http://localhost:3000", options);
            client.On(Socket.EVENT_CONNECT, () =>
            Console.WriteLine("Connected"));

            client.On(Socket.EVENT_CONNECT_ERROR, (Data) => Console.WriteLine("Connect Error: " + Data));
            client.On(Socket.EVENT_CONNECT_TIMEOUT, (Data) => Console.WriteLine("Connect TImeout Error: " + Data));

            client.On(Socket.EVENT_ERROR, (Data) => Console.WriteLine("Error: " + Data));
            client.Connect();

如果我只使用 websocket 传输,我在 Firecamp 和 C# 中都会超时。如果我启用轮询,我会收到以下错误:

    Error: Quobject.EngineIoClientDotNet.Client.EngineIOException: xhr poll error ---> System.AggregateException: One or more errors occurred. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
   at System.Net.HttpWebRequest.GetResponse()
   at Quobject.EngineIoClientDotNet.Client.Transports.PollingXHR.XHRRequest.<Create>b__7_0()
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Quobject.EngineIoClientDotNet.Client.Transports.PollingXHR.XHRRequest.Create()
   --- End of inner exception stack trace ---

我可以切换哪些其他配置设置来尝试让我的 Firecamp 或 C# 连接显示在我的 JS 服务器中?我收到来自轮询传输的“XHR 轮询错误”,以及来自 websocket 传输的超时。是否有其他调试信息可以用来确定我的问题所在?我认为,如果我可以在 Firecamp 或 C# 中进行通信,我应该能够使其在其他环境中正常工作。

【问题讨论】:

    标签: websocket socket.io


    【解决方案1】:

    我假设您使用的是 SocketIO v3 客户端。 Firecamp 仅支持 SocketIO v2。但好消息是在短短两天内,Firecamp 将在新的金丝雀版本中支持 SocketIO v3。我会在这里通知你。


    于 21 年 9 月 7 日编辑

    Firecamp is now supporting SocketIO v2, v3, and v4.

    【讨论】:

    • 太好了,谢谢!该工具非常强大且有用,让创作者回答我的问题让我有点惊讶。再次感谢您
    • 我安装了 socket.io 2.4.1,我的服务器现在在 Firecamp 中工作,谢谢你的提示!
    • @Nishchit Dhanani 不错的选择,更新推出了吗?我在哪里可以找到有关此的更多信息?
    • SocketIO v3 支持现在在私人金丝雀版本下可用,请加入 Discord 服务器并私信我以获取访问权限。 @Huzaifa 和 Jalconvolvon2 discord.gg/8hRaqhK
    【解决方案2】:

    如上所述,Firecamp 尚未针对新版本的 Socket.IO (v4) 进行优化。
    因此同时您可以选择手动启用对 Socket.IO v2 客户端的兼容性。
    您所要做的就是将“allowEIO3: true”(不带引号)作为键:值对添加到选项对象,并在创建服务器时传递此对象。
    这将允许您通过 Firecamp 与服务器通信。

    来源https://socket.io/docs/v4/server-api/#Server

    您将在下面找到与 express 服务器集成的工作 socket.io 服务器的示例。

    const app = require('express')();
    const httpServer = require('http').createServer(app);
    const options = {
          allowEIO3: true,
     };
    const io = require('socket.io')(httpServer, options);
    
    app.get('/', (req, res) => {
        res.send('home endpoint');
    });
    
    io.on('connection', (socket) => {
       socket.on('new-connection', (data) => {
       console.log(socket.id, 'connected');
       socket.broadcast.emit('test-event', { name: data.name });
     });
    
    // when the user disconnects.. perform this
     socket.on('disconnect', () => {
        console.log(`${socket.id} disconnected`);
      });
    });
    
    const port = 3000;
    
    httpServer.listen(port, () => {
       console.log(`server running on port ${port}`);
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2021-03-18
    • 2014-08-17
    • 2022-01-15
    • 2021-03-07
    • 2013-10-30
    • 1970-01-01
    相关资源
    最近更新 更多