Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to access a websocket server that runs with SignalR Hub, from my Java desktop application.

  • There is a library that does exactly that, but it's dead. There are lots of issues and pull requests and it hasn't been updated since 2015.
  • I also found another library but, as far as I can tell, it's only for Android projects.
  • I found a SO question that says it's possible to talk directly to the SignalR server (standard websocket calls) with some workarounds, but the given link is dead.

When I tried to access the server directly with websocket, the HTTP handshaking fails (I get HTTP 200 instead of 101).

Can anyone help ?

like image 745
Christophe Broeckx Avatar asked Oct 11 '17 06:10

Christophe Broeckx


People also ask

Does SignalR use WebSockets?

SignalR. ASP.NET Core SignalR is a library that simplifies adding real-time web functionality to apps. It uses WebSockets whenever possible. For most applications, we recommend SignalR over raw WebSockets.

Can a WebSocket server send message to client?

Messages sent by the server to the client can include plain text messages, binary data, or images. Whenever data is sent, the onmessage function is fired. This event acts as a client's ear to the server. Whenever the server sends data, the onmessage event gets fired.

What is WebSocket client Java?

WebSocket is a two-way communication protocol that lets clients send and receive messages over a single connection to a server endpoint. Download a PDF of this article. WebSocket is a two-way communication protocol that lets clients send and receive messages over a single connection to a server endpoint.

What protocol is used by SignalR?

SignalR provides two built-in hub protocols: a text protocol based on JSON and a binary protocol based on MessagePack. MessagePack generally creates smaller messages compared to JSON. Older browsers must support XHR level 2 to provide MessagePack protocol support.


1 Answers

You will not be able to connect to SignalR 2.x with just bare webSocket. The reason for that is that starting the connection requires a few HTTP requests which have to be sent with in a specific order and contain specific content. I described the SignalR protocol in a blog post. After reading this post you will understand why dedicated client for SignalR 2.x existed.

Having said that - the new version of 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 on an example for the hubs layer but need to finish it.

like image 81
Pawel Avatar answered Sep 28 '22 08:09

Pawel