Seven is the number.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.1 KiB

4 years ago
  1. #if UNITY_WEBGL || UNITY_XBOXONE || WEBSOCKET
  2. using System;
  3. using System.Text;
  4. #if UNITY_WEBGL && !UNITY_EDITOR
  5. using System.Runtime.InteropServices;
  6. #else
  7. using System.Collections.Generic;
  8. using System.Security.Authentication;
  9. #endif
  10. public class WebSocket
  11. {
  12. private Uri mUrl;
  13. /// <summary>Photon uses this to agree on a serialization protocol. Either: GpBinaryV16 or GpBinaryV18. Based on enum SerializationProtocol.</summary>
  14. private string protocols = "GpBinaryV16";
  15. public WebSocket(Uri url, string protocols = null)
  16. {
  17. this.mUrl = url;
  18. if (protocols != null)
  19. {
  20. this.protocols = protocols;
  21. }
  22. string protocol = mUrl.Scheme;
  23. if (!protocol.Equals("ws") && !protocol.Equals("wss"))
  24. throw new ArgumentException("Unsupported protocol: " + protocol);
  25. }
  26. public void SendString(string str)
  27. {
  28. Send(Encoding.UTF8.GetBytes (str));
  29. }
  30. public string RecvString()
  31. {
  32. byte[] retval = Recv();
  33. if (retval == null)
  34. return null;
  35. return Encoding.UTF8.GetString (retval);
  36. }
  37. #if UNITY_WEBGL && !UNITY_EDITOR
  38. [DllImport("__Internal")]
  39. private static extern int SocketCreate (string url, string protocols);
  40. [DllImport("__Internal")]
  41. private static extern int SocketState (int socketInstance);
  42. [DllImport("__Internal")]
  43. private static extern void SocketSend (int socketInstance, byte[] ptr, int length);
  44. [DllImport("__Internal")]
  45. private static extern void SocketRecv (int socketInstance, byte[] ptr, int length);
  46. [DllImport("__Internal")]
  47. private static extern int SocketRecvLength (int socketInstance);
  48. [DllImport("__Internal")]
  49. private static extern void SocketClose (int socketInstance);
  50. [DllImport("__Internal")]
  51. private static extern int SocketError (int socketInstance, byte[] ptr, int length);
  52. int m_NativeRef = 0;
  53. public void Send(byte[] buffer)
  54. {
  55. SocketSend (m_NativeRef, buffer, buffer.Length);
  56. }
  57. public byte[] Recv()
  58. {
  59. int length = SocketRecvLength (m_NativeRef);
  60. if (length == 0)
  61. return null;
  62. byte[] buffer = new byte[length];
  63. SocketRecv (m_NativeRef, buffer, length);
  64. return buffer;
  65. }
  66. public void Connect()
  67. {
  68. m_NativeRef = SocketCreate (mUrl.ToString(), this.protocols);
  69. //while (SocketState(m_NativeRef) == 0)
  70. // yield return 0;
  71. }
  72. public void Close()
  73. {
  74. SocketClose(m_NativeRef);
  75. }
  76. public bool Connected
  77. {
  78. get { return SocketState(m_NativeRef) != 0; }
  79. }
  80. public string Error
  81. {
  82. get {
  83. const int bufsize = 1024;
  84. byte[] buffer = new byte[bufsize];
  85. int result = SocketError (m_NativeRef, buffer, bufsize);
  86. if (result == 0)
  87. return null;
  88. return Encoding.UTF8.GetString (buffer);
  89. }
  90. }
  91. #else
  92. WebSocketSharp.WebSocket m_Socket;
  93. Queue<byte[]> m_Messages = new Queue<byte[]>();
  94. bool m_IsConnected = false;
  95. string m_Error = null;
  96. public void Connect()
  97. {
  98. m_Socket = new WebSocketSharp.WebSocket(mUrl.ToString(), new string[] { this.protocols });
  99. m_Socket.SslConfiguration.EnabledSslProtocols = m_Socket.SslConfiguration.EnabledSslProtocols | (SslProtocols)(3072| 768);
  100. m_Socket.OnMessage += (sender, e) => m_Messages.Enqueue(e.RawData);
  101. m_Socket.OnOpen += (sender, e) => m_IsConnected = true;
  102. m_Socket.OnError += (sender, e) => m_Error = e.Message + (e.Exception == null ? "" : " / " + e.Exception);
  103. m_Socket.ConnectAsync();
  104. }
  105. public bool Connected { get { return m_IsConnected; } }// added by TS
  106. public void Send(byte[] buffer)
  107. {
  108. m_Socket.Send(buffer);
  109. }
  110. public byte[] Recv()
  111. {
  112. if (m_Messages.Count == 0)
  113. return null;
  114. return m_Messages.Dequeue();
  115. }
  116. public void Close()
  117. {
  118. m_Socket.Close();
  119. }
  120. public string Error
  121. {
  122. get
  123. {
  124. return m_Error;
  125. }
  126. }
  127. #endif
  128. }
  129. #endif