Continue previous: "ASP. SIGNALR Series" The first lesson to know Signalr (not to see, we suggest you look first)
I. Designated means of communication
Establishing a communication method requires a certain amount of time and client/server resources. If the function of the client is known, then the communication mode can be specified at the beginning of the client connection. The following code snippet demonstrates the use of an Ajax long polling method to start a connection, if we know that the client does not support other protocols:
connection.start({ transport: ‘longPolling‘ });
You can specify a backup method if you want the client to try to communicate in order. The following code snippet shows an attempt to use WebSocket if the failure directly uses long polling.
connection.start({ transport: [‘webSockets‘,‘longPolling‘] });
Specifies that the string constants are defined as follows:
webSockets
foreverFrame
serverSentEvents
longPolling
Two, connection and hub (Hubs)
SignalR API includes two models (for client and server communication): Persistent connection model (persistent Connections) and hub (Hubs) model
A connection represents a simple endpoint (equivalent to a single recipient, a grouped, broadcast message).
Persistent connection API (rendered in. NET code in Persistentconnection), which makes it easy for developers to use the underlying communication protocols exposed by SIGNALR
Connect to the communication model, familiar with students who are accustomed to using WCF-like.
The hub model is a high-level pipeline that is built on the connection API. SIGNALR handling of the machine convenient scheduling problem is easy, it makes the client invoke the service side of the method as simple as calling the local method. and vice versa.
Using the hubs model may be easy to understand for those who have used. NET Remoting . Using the hub can also make it easy for you to bind to strongly typed parameter methods and model bindings.
Third, the structure diagram
The following schema diagram illustrates the direct relationship between hubs and persistent connections and potential technologies.
Iv. How the Hub works
When the server-side code invokes a method on the client, a packet is transmitted in an active mode of communication that contains the name and parameters of the method to invoke (when an object is transferred as a method parameter, it serializes JSON). The client then matches the name of the method (as defined in the client code). If there is a match, the method will be executed.
We can use Fiddler. To monitor the invocation procedure of a method. Shows a method call from the server.
The method that comes from the hub is called MoveShapeHub
, the method that will be called and calledupdateShape
In this example, the name of the hub is identified by the H parameter, the method name is identified by the M parameter, and the transmitted data is identified with a parameter.
The code for the example above: High-frequency Realtime
Iv. How to choose a communication model?
Most programs should choose the Hubs API. The connection API can be used in the following scenarios:
- The format in which the actual message is sent needs to be specified
- Developers like to use the messaging and scheduling model
- An already existing application (it uses the message model)
Brother Taiwan, in order to enhance my passion for writing, to support recommendations and concerns, I wish you Yushu, imposing