如何仅使用 websockets 将 java 客户端连接到 SignalR Hub?

How to connect a java client to SignalR Hub using only websockets?

我需要从我的 Java 桌面应用程序访问与 SignalR Hub 一起运行的 websocket 服务器。

当我尝试使用 websocket 直接访问服务器时,HTTP 握手失败(我得到 HTTP 200 而不是 101)。

有人能帮忙吗?

您将无法仅使用裸 webSocket 连接到 SignalR 2.x。这样做的原因是启动连接需要一些 HTTP 请求,这些请求必须以特定顺序发送并包含特定内容。我在 blog post 中描述了 SignalR 协议。阅读这篇 post 后,您将理解为什么存在 SignalR 2.x 的专用客户端。

话虽如此 - 新版本 SignalR for ASP.NET Core no longer has this protocol and requirements and it is possible to connect to the server with just bare websockets. I created a demo a while ago showing how to do this - you can find details here. I also started working 上了 hubs 层的示例,但需要完成它。