Use SignalR to create instant messages, and use signalr to create instant messages
1. What is SignalR?
SignalR is a timely message push tool similar to. NET's WCF and WebAPI, which is used for message exchange between clients and servers.
2. What is the role of SignalR?
It can synchronize messages from an online client in real time. The client sends messages to one or more clients connected to the server through the server.
3. How to Use SignalR to develop timely message sending (under mvc)
Preparation: Create a project
Use Nuget to add reference dependencies of SignalR
Install
Configure SignalR:
To use the prior steps of signalrto configure the necessary configurations, you can open the readme.txt file automatically after installation to know how to configure SignalR.
1. Add a startup Item
2. Add the js generation folder of signalr
This is the default address. If you want to have other addresses, you can configure them in the configuration file.
Introduce the js file of signalr
In this way, the configuration is complete and can be developed ....
1. First, we create the Hub of SignalR.
2. Add a method to the hub
In this way, the server method is basically complete. Let's write the client method.
4. SignalR Principle
SignalR is mainly used to solve instant communication. Its hub is equivalent to a server. Each page that connects signalr to this server is a client, and the client uses JS through the hub. server. xxxx () can call the method in the Hub of the server, and then the method of the server is Clinents. all. XXXX () can call the XXXX () method of all clients
In the Hub of the server, you can manage each client by using the Clients attribute,
We can use the hub parent class to view
We can see that there is a Clients attribute in the hub. This attribute is a set and the element type in the set is dynamic. Why is it dynamic rather than string or object?
Because our server does not know what type will be passed by the client, the dynamic type can be used to automatically confirm the type at runtime to avoid object unpacking.
Continue viewing the BaseHub class:
It can be seen that the BaseHub class also contains operations such as Context Grounps group connection disconnection and re-connection. During the corresponding operations, these methods will be triggered
The key is coming,
How does SignalR call the service end on the client and call the client method on the server? -------------Proxy
Open the client page and press F12 to view details. We found that SignalR automatically generated a js file named hubs.
In this js file, we can see that the method defined on the server is generated on the client through proxy. The method name is the same as that on the server, and the first letter is lowercase.
The hub name specified through the specified hubName feature on the server hub is also displayed in this js file.
Now we know that when the signal project is started, a js is generated at the front end through the hub of the server, this JS file uses the proxy method to generate a corresponding js method in the front of the hub. The method name is the same, but the js method is automatically generated, and the method name is lowercase.
Each Client Connected to the hub server generates a Guid on the server as its unique identifier. the Hub of the server uses Clients to manage the client.
When writing a blog for the first time, please correct me...