Asp.net mvc signalr Simple Chat Room creation process analysis, signalr Simple Chat Room
Signalr's magic and practicality have been known for a long time, but I have never tried it myself. I just stayed in the article and read a few simple introductions, which makes it easy to use.
Since a project needs to be used in the end, I decided to write a small program for testing and practice it. The environments (versions, etc.) are different for other articles written by others, there are still some discrepancies.
Environment:Vs2013/asp.net mvc 5/signalr 2.2.1/jquery 1.10.2
First two:
The system will automatically assign an ID to the person who joins the chat room, which is the unique identifier of the person (Green indicates what the current user says, orange indicates what the person outside the current user says, for different windows, the subjects indicated by the current user are different. Don't go around ...)
The following describes the development steps:
1. Install signalr
We recommend that you use nuget for installation, which saves a lot of trouble and requires manual configuration on the Internet.
2. Modify the startup. cs file.
At the beginning, I missed this step and found that/signalr/hubs could not be generated at all times. The browser prompts/signalr/hubs 400 could not be found.
3. Create a Hub
After signalr is installed in nuget, the corresponding signal category will be available.
Name it ChatHub. You can also create a common class that inherits Microsoft. AspNet. SignalR. Hub.
Write a simple Send method (note the two method names in the red box)
4. Call
The page must reference three main scripts:
Signalr depends on jquery. Therefore, the reference of these three scripts is strictly ordered and cannot be changed.
The following is the page content (note the red box ):
ChatHub: This is the created ChatHub class. The name is generated under/signalr/hubs (the first letter of the class name is lowercase by default)
Users can also define their own
In this way, the hub name is changed to the ChatHub (the first letter is lowercase, and now it is changed to uppercase)
Chat. client. receiveMessage: The receiveMessage is the Clients defined in the ChatHub class and the Send method. all. receiveMessage. I used to read some documents. The statement here is chat. receiveMessage, but it does not work. Check the/signalr/hubs script and see the definition of the client. This should be caused by different signalr versions.
Chat. server. send: The send method defined in the ChatHub class. It may also be used for signalr of previous versions by using chat. Send.
5. End
So far, a simple development process has ended. Open two or more windows to send messages and chat.
The style is purely self-defeating, and the writing is not standardized, so it is not detailed to disturb the audio and video.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.