标签: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);
标签:name public info sys sof connect csharp head collect
原文地址:http://www.cnblogs.com/wps1012/p/7306130.html