SIGNALR Series Tutorial: Signalr Quick Start

Source: Internet
Author: User
Tags call back

---restore content starts---

This article is the first of the SIGNALR series of tutorials, this article describes how to create a SIGNALR application, how to use SIGNALR to build a simple chat room, etc., this article is referenced from: http://www.asp.net/signalr/overview/ OLDER-VERSIONS/TUTORIAL-GETTING-STARTED-WITH-SIGNALR, I will have some understanding of the instance code in this article.

First we create an ASP. NET program and use the package management console to perform "Install-package Microsoft.AspNet.SignaLR" to install the latest version of SignaLR.

We create a new class named "Chathub" and Inherit the "Hub" class. In the class we add a Send method

Using Microsoft.aspnet.signalr;namespace signalr{public    class Chathub:hub    {public        void Send (string name , string message)        {            Clients.All.broadcastMessage (name, message);        }    }}

We create a new HTML file for the name index and set it as the startup item

<! DOCTYPE html><script src= "/signalr/hubs" "></script>
<script type= "Text/javascript" > $ (function () {var chat = $.connection.chathub;//This corresponds to the hub's derivation Class chat.client.broadcastMessage = function (name, message) {//This method is equivalent to subscribing to Broadcastmessa in the Chathub class The GE method, behind the scenes executed after executing the wage money method var Encodedname = $ (' <div/> '). Text (name). HTML (); var encodedmsg = $ (' <div/> '). Text (message). HTML (); $ (' #discussion '). Append (' <li><strong> ' + encodedname + ' </strong>:   ' + enc Odedmsg + ' </li> '); }; $ (' #displayname '). Val (Prompt (' Enter your Name: ', ')); $ (' #message '). focus (); $.connection.hub.start (). Done (function () {//Establish a connection succeeded bind send message Time $ (' #sendmessage '). Click (functi On () {//Call back the Senf method Chat.server.send ($ (' #displayname '). Val (), $ (' #message '). Val ()) ; $ (' #message '). Val ("). focus (); }); }); }); </script></body>

We create a new startup file and register the SIGNALR route in the configuration method

Using microsoft.owin;using owin;using SignalR; [Assembly:owinstartup (typeof (Startup))]namespace signalr{public    class Startup    {public        void Configuration (Iappbuilder app)        {            app. MAPSIGNALR ();}}}    

  

Press F5 to run the code, you will be prompted for a user name, and you can send a message

SIGNALR Series Tutorial: Signalr Quick Start

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.