The magic and practicality of signalr have been known for a long time, but I have never tried it myself. I only stayed in the articles and read a few simple introduction texts. I feel that it is very simple and easy to use.
Since I finally had a project that needed to use it, I decided to write a small program to test it and practice it to gain true knowledge. Articles written by others may still be more or less inconsistent due to different environments (versions, etc.).
Environment: vs2013 / asp.net mvc 5 / signalr 2.2.1 / jquery 1.10.2
First two renderings:
The system will automatically assign an ID to people who join the chat room , is the unique identifier of the person (green is what the current user says, orange is what people other than the current user say. For different windows, the subject indicated by the current user is different, don’t mess around...)
The following is a rough description of the development steps:
1. Install signalr
It is recommended to use nuget to install it, which can save a lot of trouble. There are also full manual configurations online.
2. Modify the startup.cs file
At the beginning, I missed this step and found that /signalr/hubs could not be generated, and the browser prompted /signalr/hubs 400 not found
3 , Create Hub
After installing signalr on nuget, there will be a corresponding signal category
named ChatHub. You can also create a normal class and inherit Microsoft.AspNet.SignalR.Hub.
Simply write a Send method (please pay attention to the two method names in the red box)
4. To call the
page, you need to reference three main scripts:
signalr depends on jquery, so These three script references are in strict order, as shown in the figure, and the order cannot be changed.
The following is the page content (please pay attention to several places in the red box):
chatHub: This is the created ChatHub class, the name generated under /signalr/hubs (the first letter of the class name is lowercase by default)
Users can also define it themselves
In this way, the hub name becomes the set ChatHub (the original first letter is lowercase, now it is uppercase)
chat.client.receiveMessage: this The receiveMessage at is defined in the ChatHub class, Clients.All.receiveMessage under the Send method. I originally read some documents and the writing method here is chat.receiveMessage, but it has no effect. After checking the /signalr/hubs script, I see As for the definition of client, this should be caused by different signalr versions. Please pay attention when using it.
chat.server.send: The send here is the Send method defined under the ChatHub class. It may also be that for previous versions of signalr, the usage method is chat.send. Just pay attention.
5. End
This ends a simple development process. Open two or more windows, and you can send messages and chat. Give it a try
The style is purely random, and the writing is irregular and messy, so I won’t post it in detail to disturb the audience.
The above is the entire content of this article. I hope it will be helpful to everyone’s learning. I also hope that everyone will support the PHP Chinese website.
For more asp.net mvc signalr simple chat room production process analysis related articles, please pay attention to the PHP Chinese website!