static void Main(string[] args) { //輸入識別名稱 Console.Write("Please input client name: "); string clientName = Console.ReadLine(); //連線SignalR Hub var connection = new HubConnection("http://localhost:4112/"); IHubProxy commHub = connection.CreateProxy("CommHub"); //顯示Hub傳入的文字訊息 commHub.On("ShowMessage", msg => Console.WriteLine(msg)); //利用done旗標決定程式中止 bool done = false; //當Hub要求執行Exit()時,將done設為true commHub.On("Exit", () => { done = true; }); //建立連線,連線建立完成後向Hub註冊識別名稱 connection.Start().ContinueWith(task => { if (!task.IsFaulted) //連線成功時呼叫Server端方法register() commHub.Invoke("register", clientName); else done = true; }); //維持程式執行迴圈 while (!done) { Thread.Sleep(100); } //主動結束連線 connection.Stop(); }
public void Stop() { //for some reason when the process is stoping all the private instances in the object are missing //need to create specificaly for shutdown _hubConnection = new HubConnection(url); _hubConnection.ConnectionId = bus.InputAddress.Queue; _monitor = _hubConnection.CreateProxy("MonitorHub"); _hubConnection.Start().Wait(); _monitor.Invoke("Deregister", new { Server = bus.InputAddress.Machine, Endpoint = bus.InputAddress.Queue }); _hubConnection.Stop(); }
private static void RunDemoHub(HubConnection hubConnection) { var demo = hubConnection.CreateProxy("demo"); demo.On("invoke", i => { Console.WriteLine("{0} client state index -> {1}", i, demo["index"]); }); hubConnection.Start().Wait(); demo.Invoke("multipleCalls").ContinueWith(task => { Console.WriteLine(task.Exception); }, TaskContinuationOptions.OnlyOnFaulted); Task.Factory.StartNew(() => { Thread.Sleep(7000); hubConnection.Stop(); }); }
private static void RunDemoHub(HubConnection hubConnection) { var demo = hubConnection.CreateProxy("demo"); hubConnection.StateChanged += change => { Console.WriteLine(change.OldState + " => " + change.NewState); }; demo.On<int>("invoke", i => { int n = demo.GetValue<int>("index"); Console.WriteLine("{0} client state index -> {1}", i, n); }); hubConnection.Start().Wait(); demo.Invoke("multipleCalls").ContinueWith(task => { Console.WriteLine(task.Exception); }, TaskContinuationOptions.OnlyOnFaulted); Task.Factory.StartNew(() => { Thread.Sleep(7000); hubConnection.Stop(); }); }