SignalR 学习 -HelloWorld

来源:互联网 发布:本周重要财经数据 编辑:程序博客网 时间:2024/05/22 06:52

SignalR 是ASP.NET下实时推送的框架,今天用这个写了一个HelloWorld的东西,记录一下!

1.添加SignalR库到项目中;

2.创建一个hub类来推送消息内容到客户端;

3.创建一个 OWIN startup类来配置应用;

4.在页面上使用SignalR Jquery 库来推送消息和展示接收到的消息;

应用效果:



这两个浏览器模仿两个用户,一个用户发消息,其他的客户端能够自动被服务器推送消息,类似QQ的消息推送。即在手机端发送消息会推送到PC端。

1)创建项目


2.选择项目类型


3)增加SignalR hub 类

在工程上右键,增加 SignalR Hub 类,VS将自动将SingnalR类库和Jquery类库添加到工程中。

using System;using System.Web;using Microsoft.AspNet.SignalR;namespace SignalRChat{    public class ChatHub : Hub    {        public void Send(string name, string message)        {            // Call the broadcastMessage method to update clients.            Clients.All.broadcastMessage(name, message);        }    }}

4)增加OWIN startup 类

右键增加OWIN startup 类即可

using Microsoft.Owin;using Owin;[assembly: OwinStartup(typeof(SignalRChat.Startup))]namespace SignalRChat{    public class Startup    {        public void Configuration(IAppBuilder app)        {            // Any connection or hub wire up and configuration should go here            app.MapSignalR();        }    }}

5)新增html

<!DOCTYPE html><html><head>    <title>SignalR Simple Chat</title>    <style type="text/css">        .container {            background-color: #99CCFF;            border: thick solid #808080;            padding: 20px;            margin: 20px;        }    </style></head><body>    <div class="container">        <input type="text" id="message" />        <input type="button" id="sendmessage" value="Send" />        <input type="hidden" id="displayname" />        <ul id="discussion">        </ul>    </div>    <!--Script references. -->    <!--Reference the jQuery library. -->    <script src="Scripts/jquery-1.6.4.min.js" ></script>    <!--Reference the SignalR library. -->    <script src="Scripts/jquery.signalR-2.1.0.min.js"></script>    <!--Reference the autogenerated SignalR hub script. -->    <script src="signalr/hubs"></script>    <!--Add script to update the page and send messages.-->     <script type="text/javascript">        $(function () {            // Declare a proxy to reference the hub.             var chat = $.connection.chatHub;            // Create a function that the hub can call to broadcast messages.            chat.client.broadcastMessage = function (name, message) {                // Html encode display name and message.                 var encodedName = $('<div />').text(name).html();                var encodedMsg = $('<div />').text(message).html();                // Add the message to the page.                 $('#discussion').append('<li><strong>' + encodedName                    + '</strong>:  ' + 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>



0 0
原创粉丝点击