Client.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Microsoft.AspNet.SignalR.Client;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace XdCxRhDW.WebApi.SignalR
  8. {
  9. class Program
  10. {
  11. //static void Main(string[] args)
  12. //{
  13. // IHubProxy hub;
  14. // //服务端配置 Startup类中 app.MapSignalR();
  15. // //string url = "http://localhost:5000/signalr";
  16. // //服务端配置 Startup类中 app.MapSignalR("/hubs", new HubConfiguration());
  17. // string url = "http://localhost:5000/hubs";
  18. // HubConnection connection = new HubConnection(url);
  19. // connection.ConnectionSlow += Connection_ConnectionSlow;
  20. // connection.Error += Connection_Error;
  21. // connection.StateChanged += Connection_StateChanged;
  22. // ///服务端 配置 [HubName("MyTestHub")]
  23. // hub = connection.CreateHubProxy("MyTestHub");
  24. // connection.Start().Wait();
  25. // hub.On("addMessage", x =>
  26. // Console.WriteLine(x));
  27. // hub.On("SendClient", x =>
  28. // {
  29. // Console.WriteLine(x);
  30. // }
  31. // );
  32. // string groupName = Console.ReadLine();
  33. // hub.Invoke("Sign", groupName).Wait();
  34. // string sendgroupName = Console.ReadLine();
  35. // hub.Invoke("SameGroupMessage", sendgroupName).Wait();
  36. // string line = "";
  37. // while ((line = Console.ReadLine()) != null)
  38. // {
  39. // hub.Invoke("Send", "测试", line).Wait();
  40. // }
  41. // Console.ReadKey();
  42. // //connection.Stop();
  43. // //connection.Dispose();
  44. //}
  45. private static void Connection_StateChanged(StateChange obj)
  46. {
  47. if (obj.NewState == ConnectionState.Disconnected)
  48. {
  49. Console.WriteLine("连接状态为未连接啦");
  50. }
  51. }
  52. private static void Connection_Error(Exception obj)
  53. {
  54. Console.WriteLine("连接发生错误啦");
  55. }
  56. private static void Connection_ConnectionSlow()
  57. {
  58. Console.WriteLine("连接超时啦");
  59. }
  60. }
  61. }