ClientWebSocket.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using UnityEngine;
  2. using BestHTTP;
  3. using BestHTTP.WebSocket;
  4. using System;
  5. namespace DTYJ
  6. {
  7. public class ClientWebSocket
  8. {
  9. public ClientWebSocket(string address, OnWebSocketOpenDelegate onOpen, OnWebSocketMessageDelegate onMessage, OnWebSocketClosedDelegate onClose, OnWebSocketErrorDelegate onError)
  10. {
  11. this.address = address;
  12. this.onOpen = onOpen;
  13. this.onMessage = onMessage;
  14. this.onClose = onClose;
  15. this.onError = onError;
  16. }
  17. OnWebSocketOpenDelegate onOpen; OnWebSocketMessageDelegate onMessage; OnWebSocketClosedDelegate onClose; OnWebSocketErrorDelegate onError;
  18. public WebSocket webSocket;
  19. private string address;
  20. public void Connect()
  21. {
  22. // Create the WebSocket instance
  23. this.webSocket = new WebSocket(new Uri(address));
  24. #if !UNITY_WEBGL || UNITY_EDITOR
  25. this.webSocket.StartPingThread = true;
  26. //this.webSocket.StartPingThread = false;
  27. #if !BESTHTTP_DISABLE_PROXY && (!UNITY_WEBGL || UNITY_EDITOR)
  28. if (HTTPManager.Proxy != null)
  29. this.webSocket.OnInternalRequestCreated = (ws, internalRequest) => internalRequest.Proxy = new HTTPProxy(HTTPManager.Proxy.Address, HTTPManager.Proxy.Credentials, false);
  30. #endif
  31. #endif
  32. // Subscribe to the WS events
  33. this.webSocket.OnOpen += OnOpen;
  34. this.webSocket.OnMessage += OnMessageReceived;
  35. this.webSocket.OnClosed += OnClosed;
  36. this.webSocket.OnError += OnError;
  37. // Start connecting to the server
  38. this.webSocket.Open();
  39. //strBtnConnction = ("Connecting...");
  40. }
  41. #region WebSocket Event Handlers
  42. /// <summary>
  43. /// Called when the web socket is open, and we are ready to send and receive data
  44. /// </summary>
  45. void OnOpen(WebSocket ws)
  46. {
  47. //strBtnConnction = ("Á¬½Ó³É¹¦!");
  48. onOpen.Invoke(ws);
  49. }
  50. /// <summary>
  51. /// Called when we received a text message from the server
  52. /// </summary>
  53. void OnMessageReceived(WebSocket ws, string message)
  54. {
  55. //resMsg = $"{DateTime.Now.ToLongTimeString()}: {message}\n" + resMsg;
  56. onMessage?.Invoke(ws, message);
  57. }
  58. /// <summary>
  59. /// Called when the web socket closed
  60. /// </summary>
  61. void OnClosed(WebSocket ws, UInt16 code, string message)
  62. {
  63. //resMsg = (string.Format("WebSocket closed! Code: {0} Message: {1}\n", code, message)) + resMsg;
  64. webSocket = null;
  65. //strBtnConnction = "Á¬½Ó";
  66. onClose?.Invoke(ws, code, message);
  67. }
  68. /// <summary>
  69. /// Called when an error occured on client side
  70. /// </summary>
  71. void OnError(WebSocket ws, string error)
  72. {
  73. //resMsg = (string.Format("An error occured: {0}\n", error)) + resMsg;
  74. webSocket = null;
  75. //strBtnConnction = "Á¬½Ó";
  76. onError?.Invoke(ws, error);
  77. }
  78. #endregion
  79. public void SendMsg(string msg)
  80. {
  81. webSocket?.Send(msg);
  82. }
  83. }
  84. }