asp.net mvc signalr Simple chat Room Production Process Analysis _ practical skills

Source: Internet
Author: User
Tags lowercase

SIGNALR Magic, practical very early know, but have not personally to try, only stay in the article, read a few simple introduction text, feeling or very simple to use.

Because the last project needs to use it, so decided to write a small program to 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

First two effects chart:

The system will automatically assign an ID to the person who joins the chat room, 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-directed subject is not the same oh, don't get around ... )

Here's a rough description of the development steps:

1. Installation SIGNALR

Recommend the use of NuGet installation, can save a lot of things, online also have a full manual configuration.

2. Modify Startup.cs File

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

3. Create hub

NuGet installed 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 scripts are referenced in strict order, such as graphs, in an irreversible order.

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

Chathub: This is the Chathub class created, generated under/signalr/hubs name (default is class name first letter lowercase)

Users can also define their own

So the hub name becomes the set Chathub (the original first lowercase, now capitalized)

Chat.client.receiveMessage: Here the receivemessage, is defined in the Chathub class, the Send method Clients.All.receiveMessage, the original look at some documents, the writing here is Chat.receivem Essage, but try to have no effect, after viewing the/signalr/hubs script, see the definition of a client, this should be due to the SIGNALR version of the difference caused by the use of attention.

Chat.server.send: Here's send, which is defined under the Chathub class, send method, also may be for the previous version of the SIGNALR, the use of the method is chat.send, everyone pay attention to it.

5. End

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

Style is purely a blind-and-sell, write the irregular also messy, so do not put the details of the disturbing audio-visual.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.