How to call SignalR C # methods

I want to see if anyone knows how javascript can call server-side methods at runtime, similar to how SignalR achieves this. The reason is that I would like to use this type of architecture in my code to remove switch statements that ultimately invoke the appropriate methods.

So now I have C # WebMethod named Execute, and it takes a string parameter, which then selects the appropriate method through the switch.

public void Execute(string Method) { switch (Method) { case "doThis": doThis(); break; } } 

It would be nice to have a similar style of dynamically calling my C # method from javascript, and then the appropriate method is called. Any ideas?

I also hope that someone can go deeper than just saying: "Look at the source."

+4
source share
3 answers

How to call SignalR C # methods

It depends on the protocol, but allows you to claim that your browser (and server) supports the webSocket protocol:

You are sending (via javascript) a message:

var socket = new WebSocket("ws://..."); // the required token on the server side is present here as a parameter (+ params)

The server can (fw4.5) process this message and reflection , dynamic - it calls the method.

However, there is a catch (by design) that it performs - asynchronously :

So this code will not work:

 var a=1; var b=2; var c=myHBub.server.add(a,b); alert(c) // undefined. 

So far this one will work:

 var a=1; var b=2; myHBub.server.add(a,b).done(function (result){alert(result);}); 

How does this happen and how?

I executed the Js client method named "Join":

and here is the main performer:

http://i.stack.imgur.com/jf9Fz.jpg

enter image description here

+3
source

Drew Marsh is right that SignalR builds and compiles lambda expressions to invoke C # hub methods, but this is probably more complicated than what is needed. All you really need is a simple reflection in the following lines:

 public void Execute(string Method) { GetType().GetMethod(Method).Invoke(this, new object[]{}); } 

SignalR uses compiled lambda expressions to improve performance, but this may not be necessary for your script.

Code for compiling the expression has already been written for ASP.NET MVC, where it is called ActionMethodDispatcher , but it was not added to SignalR before version 1.1. I saw only a 1% performance gain for the main hub call, using compilation of the expression over MethodInfo.Invoke . Profiling showed that most of the time spent using SignalR hub methods is spent on serializing / deserializing JSON.

+1
source

The code is completely open, why don't you take a look? In particular, you will want to start a search in HubDispatcher :: OnReceived , in which messages appear and are tracked all the way through the DefaultHubManager , and you will end up going to HubMethodDispatcher , where the magic happens.

Ultimately, the answer to your question is that it creates a lambda expression using Expression::Lambda to call the method and compile it into a strongly typed delegate like HubMethodExecutor(IHub, object[]) by calling Expression::Compile .

0
source

Source: https://habr.com/ru/post/1486819/


All Articles