星火直播PC
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using SuperSocket.ClientEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using WebSocket4Net;
  7. namespace XHZB.Desktop.WebSocket
  8. {
  9. public class SocketClient
  10. {
  11. // 打开的窗口列表
  12. private static readonly Dictionary<Type, object> WinDic = new Dictionary<Type, object>();
  13. private static SocketClient instance;
  14. public static SocketClient getInstance()
  15. {
  16. if (instance == null)
  17. {
  18. instance = new SocketClient();
  19. }
  20. return instance;
  21. }
  22. static void Starts()
  23. {
  24. //新建客户端类
  25. //服务端IP地址 ws://192.168.1.13 如果服务端开启了ssl或者tsl 这里前缀应该改成 wss:/
  26. //服务端监听端口 1234
  27. //自定义的地址参数 可以根据地址参数来区分客户端 /lcj控制台
  28. //WSocketClient client = new WSocketClient("wss://a.lcj888.ml:54645/lcj控制台");
  29. WSocketClient client = new WSocketClient("ws://schoolwstest.xhkjedu.com/ws");
  30. //注册消息接收事件,接收服务端发送的数据
  31. client.MessageReceived += (data) => {
  32. Console.WriteLine(data);
  33. };
  34. //开始链接
  35. client.Start();
  36. Console.WriteLine("输入“c”,退出");
  37. var input = "";
  38. do
  39. {
  40. input = Console.ReadLine();
  41. //客户端发送消息到服务端
  42. client.SendMessage(input);
  43. } while (input != "c");
  44. client.Dispose();
  45. }
  46. /// <summary>
  47. /// 添加要推送消息的窗口
  48. /// </summary>
  49. /// <param name="win"></param>
  50. public void addWin(object win)
  51. {
  52. if (WinDic.ContainsKey(win.GetType()))
  53. {
  54. WinDic[win.GetType()] = win;
  55. }
  56. else
  57. {
  58. WinDic.Add(win.GetType(), win);
  59. }
  60. }
  61. /// <summary>
  62. /// 发送给需要接收的窗口
  63. /// </summary>
  64. private static void sendUserChangeToWin()
  65. {
  66. foreach (KeyValuePair<Type, object> kvp in WinDic)
  67. {
  68. if (kvp.Value is SocketCallback callback)
  69. {
  70. callback.userListChange();
  71. }
  72. }
  73. }
  74. /// <summary>
  75. /// 移除窗口
  76. /// </summary>
  77. /// <param name="win"></param>
  78. public void removedWin(object win)
  79. {
  80. if (WinDic.ContainsKey(win.GetType()))
  81. {
  82. WinDic.Remove(win.GetType());
  83. }
  84. }
  85. }
  86. }