Chat Demo SignalR

Source: Internet
Author: User
Tags call back jquery library

1 First this demo is for NET version is 4.5 SignalR get 2.2 of

2 Create a new MVC project

3 Nuget Search SignalR installation of an item

4 Create a new hub class

To modify a newly created class

     [Hubname ("demo")]      Public class Chathub:hub    {        publicvoid Send (stringstring  message)        {             // Call the Addnewmessagetopage method to update clients.             Clients.All.addNewMessageToPage (name, message);        }    }

5 Startup modification

 Public void Configuration (Iappbuilder app)        {            //Configureauth (APP);             app. MAPSIGNALR ();        }

6 in the Home/index

<Divclass= "Container">    <inputtype= "text"ID= "message" />    <inputtype= "button"TabIndex= "1"ID= "SendMessage"value= "Send" />    <inputtype= "hidden"ID= "DisplayName" />    <ulID= "discussion"></ul></Div>@section Scripts {<!--Script references. -    <!--The JQuery Library is required and was referenced by default in _layout.cshtml. -    <!--Reference the SignalR library. -    @*<Scriptsrc= "~/scripts/jquery.signalr-1.0.1.js"></Script>*@    <Scriptsrc= "~/scripts/jquery-1.10.2.js"></Script>    <Scriptsrc= "~/scripts/jquery.signalr-2.2.0.js"></Script>    <Scriptsrc= "~/signalr/hubs"></Script>    <!--Reference the autogenerated SignalR hub script. -    <!--SignalR script to update the chat page and send messages. -    <Script>        $(function () {            //Reference The auto-generated proxy for the hub.            varChat=$.connection.demo; //Create a function, the hub can call back to display messages.Chat.client.addNewMessageToPage= function(name, message) {//ADD the message to the page.                $('#discussion'). Append ('<li><strong>' +HtmlEncode (name)+ '</strong>:' +htmlEncode (message)+ '</li>');            }; //Get the user name and store it to prepend to messages.            $('#displayname'). Val (Prompt ('Enter your name:', "')); //Set initial focus to message input box.            $('#message'). focus (); //Start the connection.$.connection.hub.start (). Done (function () {                $('#sendmessage'). Click (function () {                    //Call the Send method on the hub.Chat.server.send ($ ('#displayname'). Val (), $ ('#message'). Val ()); //Clear text box and reset focus for next comment.                    $('#message'). Val ("'). focus ();            });        });        }); //This optional function html-encodes messages for display in the page.        functionHtmlEncode (value) {varEncodedvalue= $('<div/>'). Text (value). html (); returnEncodedvalue; }    </Script>}

This is the website demo is the display interface nothing to say

Also attached official website download Zip All the powerful features have!!

More powerful demo on ASP. SignalR website

The Project Demo

Download

Chat Demo SignalR

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.