问题描述
我正在尝试设置一个非常基本的 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.io
、ForceNew = True
和 Transports = 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# 中进行通信,我应该能够在其他环境中进行通信。
解决方法
我假设您使用的是 SocketIO v3 客户端。 Firecamp 仅支持 SocketIO v2。但好消息是,在短短两天内,Firecamp 将在新的 Canary 版本中支持 SocketIO v3。我会在这里通知你。
,如上所述,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}`);
});