1. 在代码中调用Hub
找到一种方法存储下connectionId,在代码中就可以通过其他凭据调用前台。
public class HubHandler { private readonly static Lazy<HubHandler> _instance = new Lazy<HubHandler>( () => new HubHandler(GlobalHost.ConnectionManager.GetHubContext<MyHub>())); private IHubContext _context; public static HubHandler Default => _instance.Value; private HubHandler(IHubContext context) { _context = context; } public void Enter(string connectionId) { _context.Clients.Client(connectionId).enter(); } }
2.jquery版本、执行server端代码
<script src="~/Scripts/jquery-2.2.4.min.js"></script> <!--使用jquery3.x会有问题--> <script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script> <script src="/signalr/hubs"></script> <script> $(function () { var hub = $.connection.myHub; hub.client.enter = function () { console.log("enter"); }; $.connection.hub.start().done(function () { //调用server的代码请等待启动完成 hub.server.connectTest("myid"); }); }); </script>