Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR - connect to websocket service from javascript without SignalR library

I have a small SignalR project that I've started that right now all it does is receives a string and echo it back to all connected users.

What I'm wondering is, since SignalR open websockets on my server - how can I connect to the service using regular websockets javascript code? (I have a reason for wanting to do it that way without the SignalR library).

I've watched using chrome developer tools and I found that the address the browser is connecting to is:

ws://localhost:53675/signalr/connect?transport=webSockets&clientProtocol=1.4&connectionToken=YKgNxA6dhmtHya1srzPPIv6KFIYEjLTFOogXqcjPErFcKCmRdjvS2X6A2KmraW%2BrLnRUNf68gYPdOkOOgJ8yRcq4iCDm%2BbUyLejsr2NySNZBvLloWuMIAvgI6oC%2Fyt%2Ba&connectionData=%5B%7B%22name%22%3A%22ophirhubtest%22%7D%5D&tid=7

How do I generate the token?

Then, it seems that the messages going between the client and server are just regular json formatted text (which will be easy to mimic):

{"C":"d-9E7D682A-A,1|E,0|F,1|G,0","S":1,"M":[]}
{"H":"ophirhubtest","M":"Echo","A":["test"],"I":0}
{"C":"d-9E7D682A-A,2|E,0|F,1|G,0","M":[{"H":"ophirHubTest","M":"printEcho","A":["You said: test"]}]}

If I just try to connect than it connects but the connection is quickly closed. If I remove the token it closes immediately.

Is it possible to connect to the WS "manually" ?

like image 627
developer82 Avatar asked Jul 21 '15 14:07

developer82


People also ask

Does SignalR require WebSockets?

ASP.NET Core SignalR is a library that simplifies adding real-time web functionality to apps. It uses WebSockets whenever possible.

What is difference between SignalR and WebSocket?

WebSockets is actually the underlying transport that SignalR uses, at least most of the time. SignalR has the ability to fall back to other ways of transporting messages, such as long-polling over HTTP. This is useful in situations where you don't have WebSockets support.


2 Answers

Before you can connect to the server there is connection negotiation happening. This is when the server sends all the data needed to send and receive messages. Without connection negotiation you won't be able to connect to the server. Once you implement connection negotiation you will be probably half into implementing a SignalR client. I wrote a blog post describing SignalR protocol which should help you understand how things works underneath and why connecting to the server with your own websocket is not straightforward (or simply impossible if you don't follow the SignalR protocol).

EDIT

The ASP.NET Core version of SignalR now allows connecting to the server with bare webSocket.

like image 167
Pawel Avatar answered Oct 05 '22 23:10

Pawel


I just want to add a that it is possible to connect to ASP.NET Core version of SignalR with websocket but you have to add the magic char 30 at the end of every call you make

const endChar = String.fromCharCode(30);
socket.send(`{"arguments":["arg1"],"streamIds":[],"target":"TestMethod","type":1}${endChar}`);
like image 41
Frédéric Thibault Avatar answered Oct 06 '22 00:10

Frédéric Thibault