Analysis of ASP. NET MVC signalr simple chat room production process

Source: Internet
Author: User
SIGNALR Magic, practical very early know, but have not personally to try, only stay in the article, read a few simple introduction text, feel is very simple and easy to use.

Because the last project needs to use it, so decided to write a small program test, practice the truth, others write articles, due to the environment (version, etc.) different, or more or less there are some discrepancies.

Environment: vs2013/asp.net MVC 5/signalr 2.2.1/jquery 1.10.2

Top two first:

The system automatically assigns the person who joins the chat room an ID, is the person's unique identification (green for the current user said, orange for the current user outside the words, for different windows, the current user indicates that the main body is not the same oh, don't get around ...) )

Here's a rough look at the development steps:

1, installation SIGNALR

It is recommended to use NuGet installation, can save a lot of things, online also have a fully hand-configured.

2. Modify the Startup.cs file

At the beginning, I missed this step, found that always can not generate/signalr/hubs, browser prompt/signalr/hubs 400 cannot find

3. Create hub

After NuGet installs the SIGNALR, there will be a corresponding signal classification

Named Chathub, you can also create a generic class that inherits Microsoft.AspNet.SignalR.Hub.

Simply write a Send method (note the two method names of the red box)

4. Call

The page needs to refer to three main scripts:

SIGNALR is dependent on jquery, so these three script references are in strict order and cannot be reversed.

The following is the content of the page (please note several places in the red box):

Chathub: This is the Chathub class created, generating the name under/signalr/hubs (default to the first lowercase of the class name)

Users can also define their own

This way the hub name becomes the set Chathub (the original lowercase letter, which now becomes uppercase)

Chat.client.receiveMessage: Here the receiveMessage, is defined in the Chathub class, the Send method under the Clients.All.receiveMessage, the original look at some documents, here is the wording of Chat.receivem Essage, but try to have no effect, look at the/signalr/hubs script, see the definition of the client, this piece should be due to the SIGNALR version of the different cause, we should pay attention to when using.

Chat.server.send: Send here, is defined in the Chathub class of the Send method, the same may be for the previous version of the SIGNALR, the use of chat.send, everyone notice.

5. End

So far, a simple development process has ended. Open two or two more windows, you can send a message chat, try it

The style is purely own blind daoteng, write not standard also disorderly, so do not detailed put out disturb the audio-visual.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.


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.