SignalR Simple Example

Source: Internet
Author: User
Tags html encode

Original: SignalR Simple example

First, what is SignalR

  ASP. SignalR is a library for ASP. Developers that simplifies the process of adding real-time web Functiona Lity to applications. Real-time Web functionality is the ability to has server code push content to connected clients instantly as it becomes a Vailable, rather than has the server wait for a client to request new data.

Second, a simple example

New Project Signalrchat

Select the Empty template

Installing SignalR

After adding the view Scripts folder

Add Signal Hub Class (V2)

The code is as follows

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingMicrosoft.AspNet.SignalR;namespacesignalrchat{ Public classChathub:hub { Public voidSend (stringNamestringmessage)        {Clients.All.broadcastMessage (name, message); }    }}

Add OWIN Startup class

The code is as follows

usingSystem;usingSystem.Threading.Tasks;usingMicrosoft.owin;usingOwin; [Assembly:owinstartup (typeof(Signalrchat.startup))]namespacesignalrchat{ Public classStartup { Public voidConfiguration (Iappbuilder app) {//For more information on how to configure your application, visithttp://go.microsoft.com/fwlink/?LinkID=316888app.        MAPSIGNALR (); }    }}

Add HTML page

The code is as follows

<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head>    <title>SignalR Simple Chat</title>    <styletype= "Text/css">. Container{Background-color:#99CCFF;Border:Thick solid #808080;padding:20px;margin:20px;        }    </style></Head><Body>    <Divclass= "Container">        <inputtype= "text"ID= "message" />        <inputtype= "button"ID= "SendMessage"value= "Send" />        <inputtype= "hidden"ID= "DisplayName" />        <ulID= "discussion"></ul>    </Div>    <!--Script references. -    <!--Reference the JQuery library. -    <Scriptsrc= "Scripts/jquery-1.6.4.min.js"></Script>    <!--Reference the SignalR library. -    <Scriptsrc= "Scripts/jquery.signalr-2.2.0.min.js"></Script>    <!--Reference the autogenerated SignalR hub script. -    <Scriptsrc= "Signalr/hubs"></Script>    <!--Add script to update the page and send messages. -    <Scripttype= "Text/javascript">        $(function () {            //to enable logging for your hub's events in a browser, add the following command to your client application$.connection.hub.logging= true; //Declare A proxy to reference the hub.            varChat=$.connection.chathub; //Create a function, the hub can call to broadcast messages.Chat.client.broadcastMessage= function(name, message) {//Html encode display name and message.                varEncodedname= $('<div/>'). Text (name). HTML (); varencodedmsg= $('<div/>'). Text (message). HTML (); //ADD the message to the page.                $('#discussion'). Append ('<li><strong>' +Encodedname+ '</strong>:&nbsp;&nbsp;' +encodedmsg+ '</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 ();            });        });    }); </Script></Body></HTML>

  F5 Run, copy URL and open a new browser window to run simultaneously, enter Nameone and nametwo respectively

If it is IE Browser, open soultion Explorer, you can see the hubs file

  F12 Open the console and discover that you are using foreverframe (if you want to view the log in the console, the code must contain $.connection.hub.logging = true;)

  The Chrome effect is as follows

Reference article: HTTP://WWW.ASP.NET/SIGNALR/OVERVIEW/GETTING-STARTED/TUTORIAL-GETTING-STARTED-WITH-SIGNALR

Code download

Signalrchat

SignalR Simple Example

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.