SignalR Selfhost Real-time messaging, integrated into the Web for server message push

Source: Internet
Author: User

Previously used two times signalr, but there was a period of time did not get, today re-get, found that has forgotten almost, make a note!

Create a console project first
NuGet Add Reference Online Search:
Microsoft.AspNet.SignalR.SelfHost
Microsoft.Owin.Cors

Add code in Program.cs


Add a Hub

In this hub, we've got a connection. When the server pushes a message like a client, the client receives the method as receive
Next I added a Send method to the hub, the client sends the message when the message will be broadcast to all clients, the code is as follows:

public void Send (String msg)        {            Clients.All.receive (msg);        }

  



Let's test the Web side first:
Create an empty web MVC project (WinForm is OK), NuGet add search SignalR Add: Reference to Microsoft ASP. SignalR JavaScript Client


Create a homecontroller that is referenced in its index.cshtml, respectively: jquery,jquery.signalr.xxx.min.js
Add JS Reference and JS code

Note the two HTTP address references, be sure to write the address of your service point with the port, next we test the effect:

From the above we can see that no matter which connection to the server, send the message, all the clients can receive, if you want to send a message for a separate person, only need to specify the person's ClientID can
SIGNALR will not save the client information, do not know if the person is connected to the end, so to achieve online offline function, maintain an online cache table!

Click here to download

SignalR Selfhost Real-time messaging, integrated into the Web for server message push

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.