• SignalR小计


    微软官方例子地址:http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc

    1、作用:

      服务器端推送信息或数据到客户端,比如:你在数据库中新增了一条数据,如果客户端不进行重新访问,客户端是不能及时收到数据的。以前的做法是使用ajax等方式。SignalR的出现就是解决这个问题的,服务器端直接推送数据到客户端。

    2、安装SignalR:

        可以使用Nuget控制台安装(install-package Microsoft.AspNet.SignalR),或者使用"扩展和更新"搜索安装。安装完成会引入程序集及客户端所用到的js脚本。

    3、新建(MVC项目):

        新建owin Startup类,注意([assembly: OwinStartup(typeof(SignalRChat.Startup))]),

     1 using Owin;
     2 using Microsoft.Owin;
     3 [assembly: OwinStartup(typeof(SignalRChat.Startup))]
     4 namespace SignalRChat
     5 {
     6     public class Startup
     7     {
     8         public void Configuration(IAppBuilder app)
     9         {
    10             // Any connection or hub wire up and configuration should go here
    11             app.MapSignalR();
    12         }
    13     }
    14 }

    4、新建SignalR Hub类文件:

          

     1 using System;
     2 using System.Web;
     3 using Microsoft.AspNet.SignalR;
     4 namespace SignalRChat
     5 {
     6     public class ChatHub : Hub
     7     {
     8         public void Send(string name, string message)
     9         {
    10             // Call the addNewMessageToPage method to update clients.
    11             Clients.All.addNewMessageToPage(name, message);
    12         }
    13     }
    14 }

        此步骤注意几点:①Send方法,待会客户端js中会调用 ②addNewMessageToPage方法为在客户端定义的方法 ③新建的Hub类要继承Hub,类名要记着在客户端会用到。

    5、客户端js

        

     <script src="~/Scripts/jquery.signalR-2.1.0.min.js"></script>
        <!--Reference the autogenerated SignalR hub script. -->
        <script src="~/signalr/hubs"></script>
    $(function () {
                // Reference the auto-generated proxy for the hub.  
                var chat = $.connection.chatHub;
                // Create a function that 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.
            function htmlEncode(value) {
                var encodedValue = $('<div />').text(value).html();
                return encodedValue;
            }

        注意几点:①$.connection.chatHub此处的chatHub为服务器端定义的Hub类名,并且在js中首字母要小写

             ②addNewMessageToPage服务器端用到的方法  

             ③chat.server.send($('#displayname').val(), $('#message').val());send方法是服务器端定义的方法

             ④导入js <script src="~/signalr/hubs"></script>地址为hub的地址,如果hub不在同一服务器要把~换成hub所在服务器地址,

              例如"http://localhost:8080/signalr/hubs"



        

  • 相关阅读:
    springboot 上传文件过大的500异常
    java OSS批量下载,并压缩为ZIP
    Java 对象转xml (dom 4j)
    windows 10 64位机器上 安装部署
    Java 读取excel 文件流
    关于Java 去除空格,换行的代码
    ORACLE 查询近一天,近半小时内的数据
    【转】C#(ASP.Net)获取当前路径的方法集合
    【转】NumPy-快速处理数据
    【转】Eclipse 常用快捷键 (动画讲解)
  • 原文地址:https://www.cnblogs.com/zhaoyihao/p/4644022.html
Copyright © 2020-2023  润新知