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.

184 lines
5.1 KiB

3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
  1. #if UNITY_WEBGL || WEBSOCKET || ((UNITY_XBOXONE || UNITY_GAMECORE) && UNITY_EDITOR)
  2. // --------------------------------------------------------------------------------------------------------------------
  3. // <summary>
  4. // Provided originally by Unity to cover WebSocket support in WebGL and the Editor. Modified by Exit Games GmbH.
  5. // </summary>
  6. // <author>developer@exitgames.com</author>
  7. // --------------------------------------------------------------------------------------------------------------------
  8. using System;
  9. using System.Text;
  10. #if UNITY_WEBGL && !UNITY_EDITOR
  11. using System.Runtime.InteropServices;
  12. #else
  13. using WebSocketSharp;
  14. using System.Collections.Generic;
  15. using System.Security.Authentication;
  16. #endif
  17. public class WebSocket
  18. {
  19. private Uri mUrl;
  20. /// <summary>Photon uses this to agree on a serialization protocol. Either: GpBinaryV16 or GpBinaryV18. Based on enum SerializationProtocol.</summary>
  21. private string protocols = "GpBinaryV16";
  22. public WebSocket(Uri url, string serialization = null)
  23. {
  24. this.mUrl = url;
  25. if (serialization != null)
  26. {
  27. this.protocols = serialization;
  28. }
  29. string protocol = mUrl.Scheme;
  30. if (!protocol.Equals("ws") && !protocol.Equals("wss"))
  31. throw new ArgumentException("Unsupported protocol: " + protocol);
  32. }
  33. public void SendString(string str)
  34. {
  35. Send(Encoding.UTF8.GetBytes (str));
  36. }
  37. public string RecvString()
  38. {
  39. byte[] retval = Recv();
  40. if (retval == null)
  41. return null;
  42. return Encoding.UTF8.GetString (retval);
  43. }
  44. #if UNITY_WEBGL && !UNITY_EDITOR
  45. [DllImport("__Internal")]
  46. private static extern int SocketCreate (string url, string protocols);
  47. [DllImport("__Internal")]
  48. private static extern int SocketState (int socketInstance);
  49. [DllImport("__Internal")]
  50. private static extern void SocketSend (int socketInstance, byte[] ptr, int length);
  51. [DllImport("__Internal")]
  52. private static extern void SocketRecv (int socketInstance, byte[] ptr, int length);
  53. [DllImport("__Internal")]
  54. private static extern int SocketRecvLength (int socketInstance);
  55. [DllImport("__Internal")]
  56. private static extern void SocketClose (int socketInstance);
  57. [DllImport("__Internal")]
  58. private static extern int SocketError (int socketInstance, byte[] ptr, int length);
  59. int m_NativeRef = 0;
  60. public void Send(byte[] buffer)
  61. {
  62. SocketSend (m_NativeRef, buffer, buffer.Length);
  63. }
  64. public byte[] Recv()
  65. {
  66. int length = SocketRecvLength (m_NativeRef);
  67. if (length == 0)
  68. return null;
  69. byte[] buffer = new byte[length];
  70. SocketRecv (m_NativeRef, buffer, length);
  71. return buffer;
  72. }
  73. public void Connect()
  74. {
  75. m_NativeRef = SocketCreate (mUrl.ToString(), this.protocols);
  76. //while (SocketState(m_NativeRef) == 0)
  77. // yield return 0;
  78. }
  79. public void Close()
  80. {
  81. SocketClose(m_NativeRef);
  82. }
  83. public bool Connected
  84. {
  85. get { return SocketState(m_NativeRef) != 0; }
  86. }
  87. public string Error
  88. {
  89. get {
  90. const int bufsize = 1024;
  91. byte[] buffer = new byte[bufsize];
  92. int result = SocketError (m_NativeRef, buffer, bufsize);
  93. if (result == 0)
  94. return null;
  95. return Encoding.UTF8.GetString (buffer);
  96. }
  97. }
  98. #else
  99. WebSocketSharp.WebSocket m_Socket;
  100. Queue<byte[]> m_Messages = new Queue<byte[]>();
  101. bool m_IsConnected = false;
  102. string m_Error = null;
  103. public void Connect()
  104. {
  105. m_Socket = new WebSocketSharp.WebSocket(mUrl.ToString(), new string[] { this.protocols });
  106. m_Socket.SslConfiguration.EnabledSslProtocols = m_Socket.SslConfiguration.EnabledSslProtocols | (SslProtocols)(3072| 768);
  107. m_Socket.OnMessage += (sender, e) => m_Messages.Enqueue(e.RawData);
  108. m_Socket.OnOpen += (sender, e) => m_IsConnected = true;
  109. //this.m_Socket.Log.Level = LogLevel.Debug;
  110. //this.m_Socket.Log.Output += Output;
  111. this.m_Socket.OnClose += SocketOnClose;
  112. m_Socket.OnError += (sender, e) => m_Error = e.Message + (e.Exception == null ? "" : " / " + e.Exception);
  113. m_Socket.ConnectAsync();
  114. }
  115. private void SocketOnClose(object sender, CloseEventArgs e)
  116. {
  117. //UnityEngine.Debug.Log(e.Code.ToString());
  118. // this code is used for cases when the socket failed to get created (specifically used to detect "blocked by Windows firewall")
  119. // for some reason this situation is not calling OnError
  120. if (e.Code == 1006)
  121. {
  122. this.m_Error = e.Reason;
  123. this.m_IsConnected = false;
  124. }
  125. }
  126. public bool Connected { get { return m_IsConnected; } }// added by TS
  127. public void Send(byte[] buffer)
  128. {
  129. m_Socket.Send(buffer);
  130. }
  131. public byte[] Recv()
  132. {
  133. if (m_Messages.Count == 0)
  134. return null;
  135. return m_Messages.Dequeue();
  136. }
  137. public void Close()
  138. {
  139. m_Socket.Close();
  140. }
  141. public string Error
  142. {
  143. get
  144. {
  145. return m_Error;
  146. }
  147. }
  148. #endif
  149. }
  150. #endif