Asp.net mvc signalr simple chat room, mvcsignalr

Source: Internet
Author: User

Asp.net mvc signalr simple chat room, mvcsignalr

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 is defined in the ChatHub class. It may also be used for the previous version of signalr. You should pay attention to it.

 

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.