从 C# 程序连接基于 node.js 的 socket.io WebSocket 服务器

问题描述:

我需要从 C# windows 窗体代码连接基于 Node.js 的 WebSocket

I need to connect Node.js based WebSocket from a C# windows form code

使用的节点模块https://github.com/Automattic/socket.io

我正在使用 superwebsocket 和 WebSocket4Net

I am using superwebsocket and WebSocket4Net

using SuperSocket.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Logging;
using SuperWebSocket;
using SuperWebSocket.SubProtocol;
using WebSocket4Net;

......
......

WebSocket webSocketClient = new WebSocket("ws://localhost:8080/");
webSocketClient.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(webSocketClient_Error);
webSocketClient.AllowUnstrustedCertificate = true;
webSocketClient.Opened += new EventHandler(webSocketClient_Opened);
webSocketClient.Closed += new EventHandler(webSocketClient_Closed);
webSocketClient.MessageReceived += new EventHandler<MessageReceivedEventArgs>(webSocketClient_MessageReceived);
webSocketClient.Open();

但只有 webSocketClient_Error 回调总是触发,有人可以帮忙吗?

But only webSocketClient_Error callback triggers always, can anyone help?

Node.JS socket.io 库使用不同的 URL 模式,但您已提供根 URL,请按如下方式更改您的 URL,然后它应该可以工作

Node.JS socket.io library uses a different URL pattern but you have given root URL, Please change your URL as follows then it should work

WebSocket webSocketClient = new WebSocket("ws://localhost:8080/socket.io/?EIO=3&transport=websocket");