---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