码迷,mamicode.com
首页 > 其他好文 > 详细

signalR

时间:2017-08-08 14:06:46      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:name   public   info   sys   sof   connect   csharp   head   collect   

1.添加signalR包

2.添加Startup类

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(signalR.Startup))]

namespace signalR
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
            //注册signalr/hubs
            app.MapSignalR();
        }
    }
}

 3.添加MyHub类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace signalR
{
    public class MyHub : Hub
    {
        public void Hello(string message)
        {
            Clients.All.hello(message);
        }
    }
}

 4.前台js引用并实现

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="Scripts/jquery-1.6.4.min.js"></script>
    <script src="Scripts/jquery.signalR-2.2.2.min.js"></script>
    <script src="signalr/hubs"></script>
    <script>
        $(function () {
            var hellohub = $.connection.myHub;
            hellohub.client.hello = function (message) {
                $("#text").append("<p>" + message + "</P>");
            };
            $.connection.hub.start().done(function () {
                $("#send").click(function () {
                    hellohub.server.hello("testmessage");
                })
            });
        })        
    </script>
</head>
<body>
    <input id="send" type="button" value="send" />
    <div id="text"></div>
</body>
</html>

 5.后台调用代码

Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<MyHub>().Clients.All.hello(content);

 

signalR

标签:name   public   info   sys   sof   connect   csharp   head   collect   

原文地址:http://www.cnblogs.com/wps1012/p/7306130.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!