init
This commit is contained in:
435
Assets/Photon/PhotonLibs/WebSocket/SocketWebTcp.cs
Normal file
435
Assets/Photon/PhotonLibs/WebSocket/SocketWebTcp.cs
Normal file
@@ -0,0 +1,435 @@
|
||||
#if UNITY_WEBGL || WEBSOCKET || WEBSOCKET_PROXYCONFIG
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="SocketWebTcp.cs" company="Exit Games GmbH">
|
||||
// Copyright (c) Exit Games GmbH. All rights reserved.
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// Internal class to encapsulate the network i/o functionality for the realtime library.
|
||||
// </summary>
|
||||
// <author>developer@exitgames.com</author>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace ExitGames.Client.Photon
|
||||
{
|
||||
using System;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Scripting;
|
||||
using SupportClassPun = SupportClass;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Yield Instruction to Wait for real seconds. Very important to keep connection working if Time.TimeScale is altered, we still want accurate network events
|
||||
/// </summary>
|
||||
public sealed class WaitForRealSeconds : CustomYieldInstruction
|
||||
{
|
||||
private readonly float _endTime;
|
||||
|
||||
public override bool keepWaiting
|
||||
{
|
||||
get { return this._endTime > Time.realtimeSinceStartup; }
|
||||
}
|
||||
|
||||
public WaitForRealSeconds(float seconds)
|
||||
{
|
||||
this._endTime = Time.realtimeSinceStartup + seconds;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Internal class to encapsulate the network i/o functionality for the realtime libary.
|
||||
/// </summary>
|
||||
public class SocketWebTcp : IPhotonSocket, IDisposable
|
||||
{
|
||||
private WebSocket sock;
|
||||
|
||||
private readonly object syncer = new object();
|
||||
|
||||
[Preserve]
|
||||
public SocketWebTcp(PeerBase npeer) : base(npeer)
|
||||
{
|
||||
this.ServerAddress = npeer.ServerAddress;
|
||||
this.ProxyServerAddress = npeer.ProxyServerAddress;
|
||||
if (this.ReportDebugOfLevel(DebugLevel.INFO))
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.INFO, "new SocketWebTcp() for Unity. Server: " + this.ServerAddress + (String.IsNullOrEmpty(this.ProxyServerAddress) ? "" : ", Proxy: " + this.ProxyServerAddress));
|
||||
}
|
||||
|
||||
//this.Protocol = ConnectionProtocol.WebSocket;
|
||||
this.PollReceive = false;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.State = PhotonSocketState.Disconnecting;
|
||||
|
||||
if (this.sock != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (this.sock.Connected)
|
||||
{
|
||||
this.sock.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.EnqueueDebugReturn(DebugLevel.INFO, "Exception in SocketWebTcp.Dispose(): " + ex);
|
||||
}
|
||||
}
|
||||
|
||||
this.sock = null;
|
||||
this.State = PhotonSocketState.Disconnected;
|
||||
}
|
||||
|
||||
GameObject websocketConnectionObject;
|
||||
|
||||
public override bool Connect()
|
||||
{
|
||||
//bool baseOk = base.Connect();
|
||||
//if (!baseOk)
|
||||
//{
|
||||
// return false;
|
||||
//}
|
||||
|
||||
|
||||
this.State = PhotonSocketState.Connecting;
|
||||
|
||||
|
||||
if (this.websocketConnectionObject != null)
|
||||
{
|
||||
UnityEngine.Object.Destroy(this.websocketConnectionObject);
|
||||
}
|
||||
|
||||
this.websocketConnectionObject = new GameObject("websocketConnectionObject");
|
||||
MonoBehaviour mb = this.websocketConnectionObject.AddComponent<MonoBehaviourExt>();
|
||||
this.websocketConnectionObject.hideFlags = HideFlags.HideInHierarchy;
|
||||
UnityEngine.Object.DontDestroyOnLoad(this.websocketConnectionObject);
|
||||
|
||||
|
||||
this.ConnectAddress += "&IPv6"; // this makes the Photon Server return a host name for the next server (NS points to MS and MS points to GS)
|
||||
|
||||
|
||||
// earlier, we read the proxy address/scheme and failed to connect entirely, if that wasn't successful...
|
||||
// it was either successful (using the resulting proxy address) or no connect at all...
|
||||
|
||||
// we want:
|
||||
// WITH support: fail if the scheme is wrong or use it if possible
|
||||
// WITHOUT support: use proxy address, if it's a direct value (not a scheme we provide) or fail if it's a scheme
|
||||
|
||||
string proxyServerAddress;
|
||||
if (!this.ReadProxyConfigScheme(this.ProxyServerAddress, this.ServerAddress, out proxyServerAddress))
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.INFO, "ReadProxyConfigScheme() failed. Using no proxy.");
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
this.sock = new WebSocket(new Uri(this.ConnectAddress), proxyServerAddress, this.SerializationProtocol);
|
||||
this.sock.DebugReturn = (DebugLevel l, string s) =>
|
||||
{
|
||||
if (this.State != PhotonSocketState.Disconnected)
|
||||
{
|
||||
this.Listener.DebugReturn(l, this.State + " " + s);
|
||||
}
|
||||
};
|
||||
|
||||
this.sock.Connect();
|
||||
mb.StartCoroutine(this.ReceiveLoop());
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.ERROR, "SocketWebTcp.Connect() caught exception: " + e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to read a proxy configuration defined by a address prefix. Only available to Industries Circle members on demand.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Extended proxy support is available to Industries Circle members. Where available, proxy addresses may be defined as 'auto:', 'pac:' or 'system:'.
|
||||
/// In all other cases, the proxy address is used as is and fails to read configs (if one of the listed schemes is used).
|
||||
///
|
||||
/// Requires file ProxyAutoConfig.cs and compile define: WEBSOCKET_PROXYCONFIG_SUPPORT.
|
||||
/// </remarks>
|
||||
/// <param name="proxyAddress">Proxy address from the server configuration.</param>
|
||||
/// <param name="url">Url to connect to (one of the Photon servers).</param>
|
||||
/// <param name="proxyUrl">Resulting proxy URL to use.</param>
|
||||
/// <returns>False if there is some error and the resulting proxy address should not be used.</returns>
|
||||
private bool ReadProxyConfigScheme(string proxyAddress, string url, out string proxyUrl)
|
||||
{
|
||||
proxyUrl = null;
|
||||
|
||||
#if !WEBSOCKET_PROXYCONFIG
|
||||
|
||||
if (!string.IsNullOrEmpty(proxyAddress))
|
||||
{
|
||||
if (proxyAddress.StartsWith("auto:") || proxyAddress.StartsWith("pac:") || proxyAddress.StartsWith("system:"))
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.WARNING, "Proxy configuration via auto, pac or system is only supported with the WEBSOCKET_PROXYCONFIG define. Using no proxy instead.");
|
||||
return true;
|
||||
}
|
||||
proxyUrl = proxyAddress;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
#else
|
||||
|
||||
if (!string.IsNullOrEmpty(proxyAddress))
|
||||
{
|
||||
var httpUrl = url.ToString().Replace("ws://", "http://").Replace("wss://", "https://"); // http(s) schema required in GetProxyForUrlUsingPac call
|
||||
bool auto = proxyAddress.StartsWith("auto:", StringComparison.InvariantCultureIgnoreCase);
|
||||
bool pac = proxyAddress.StartsWith("pac:", StringComparison.InvariantCultureIgnoreCase);
|
||||
|
||||
if (auto || pac)
|
||||
{
|
||||
string pacUrl = "";
|
||||
if (pac)
|
||||
{
|
||||
pacUrl = proxyAddress.Substring(4);
|
||||
if (pacUrl.IndexOf("://") == -1)
|
||||
{
|
||||
pacUrl = "http://" + pacUrl; //default to http
|
||||
}
|
||||
}
|
||||
|
||||
string processTypeStr = auto ? "auto detect" : "pac url " + pacUrl;
|
||||
|
||||
this.Listener.DebugReturn(DebugLevel.INFO, "WebSocket Proxy: " + url + " " + processTypeStr);
|
||||
|
||||
string errDescr = "";
|
||||
var err = ProxyAutoConfig.GetProxyForUrlUsingPac(httpUrl, pacUrl, out proxyUrl, out errDescr);
|
||||
|
||||
if (err != 0)
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.ERROR, "WebSocket Proxy: " + url + " " + processTypeStr + " ProxyAutoConfig.GetProxyForUrlUsingPac() error: " + err + " (" + errDescr + ")");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (proxyAddress.StartsWith("system:", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.INFO, "WebSocket Proxy: " + url + " system settings");
|
||||
string proxyAutoConfigPacUrl;
|
||||
var err = ProxySystemSettings.GetProxy(out proxyUrl, out proxyAutoConfigPacUrl);
|
||||
if (err != 0)
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.ERROR, "WebSocket Proxy: " + url + " system settings ProxySystemSettings.GetProxy() error: " + err);
|
||||
return false;
|
||||
}
|
||||
if (proxyAutoConfigPacUrl != null)
|
||||
{
|
||||
if (proxyAutoConfigPacUrl.IndexOf("://") == -1)
|
||||
{
|
||||
proxyAutoConfigPacUrl = "http://" + proxyAutoConfigPacUrl; //default to http
|
||||
}
|
||||
this.Listener.DebugReturn(DebugLevel.INFO, "WebSocket Proxy: " + url + " system settings AutoConfigURL: " + proxyAutoConfigPacUrl);
|
||||
string errDescr = "";
|
||||
err = ProxyAutoConfig.GetProxyForUrlUsingPac(httpUrl, proxyAutoConfigPacUrl, out proxyUrl, out errDescr);
|
||||
|
||||
if (err != 0)
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.ERROR, "WebSocket Proxy: " + url + " system settings AutoConfigURLerror: " + err + " (" + errDescr + ")");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
proxyUrl = proxyAddress;
|
||||
}
|
||||
|
||||
this.Listener.DebugReturn(DebugLevel.INFO, "WebSocket Proxy: " + url + " -> " + (string.IsNullOrEmpty(proxyUrl) ? "DIRECT" : "PROXY " + proxyUrl));
|
||||
}
|
||||
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override bool Disconnect()
|
||||
{
|
||||
if (this.ReportDebugOfLevel(DebugLevel.INFO))
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.INFO, "SocketWebTcp.Disconnect()");
|
||||
}
|
||||
|
||||
this.State = PhotonSocketState.Disconnecting;
|
||||
|
||||
lock (this.syncer)
|
||||
{
|
||||
if (this.sock != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.sock.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.ERROR, "Exception in SocketWebTcp.Disconnect(): " + ex);
|
||||
}
|
||||
|
||||
this.sock = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.websocketConnectionObject != null)
|
||||
{
|
||||
UnityEngine.Object.Destroy(this.websocketConnectionObject);
|
||||
}
|
||||
|
||||
this.State = PhotonSocketState.Disconnected;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// used by TPeer*
|
||||
/// </summary>
|
||||
public override PhotonSocketError Send(byte[] data, int length)
|
||||
{
|
||||
if (this.State != PhotonSocketState.Connected)
|
||||
{
|
||||
return PhotonSocketError.Skipped;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (data.Length > length)
|
||||
{
|
||||
byte[] trimmedData = new byte[length];
|
||||
Buffer.BlockCopy(data, 0, trimmedData, 0, length);
|
||||
data = trimmedData;
|
||||
}
|
||||
|
||||
//if (this.ReportDebugOfLevel(DebugLevel.ALL))
|
||||
//{
|
||||
// this.Listener.DebugReturn(DebugLevel.ALL, "Sending: " + SupportClassPun.ByteArrayToString(data));
|
||||
//}
|
||||
|
||||
if (this.sock != null)
|
||||
{
|
||||
this.sock.Send(data);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.ERROR, "Cannot send to: " + this.ServerAddress + ". " + e.Message);
|
||||
|
||||
this.HandleException(StatusCode.Exception);
|
||||
return PhotonSocketError.Exception;
|
||||
}
|
||||
|
||||
return PhotonSocketError.Success;
|
||||
}
|
||||
|
||||
public override PhotonSocketError Receive(out byte[] data)
|
||||
{
|
||||
data = null;
|
||||
return PhotonSocketError.NoData;
|
||||
}
|
||||
|
||||
|
||||
internal const int ALL_HEADER_BYTES = 9;
|
||||
internal const int TCP_HEADER_BYTES = 7;
|
||||
internal const int MSG_HEADER_BYTES = 2;
|
||||
|
||||
public IEnumerator ReceiveLoop()
|
||||
{
|
||||
//this.Listener.DebugReturn(DebugLevel.INFO, "ReceiveLoop()");
|
||||
if (this.sock != null)
|
||||
{
|
||||
while (this.sock != null && !this.sock.Connected && this.sock.Error == null)
|
||||
{
|
||||
yield return new WaitForRealSeconds(0.1f);
|
||||
}
|
||||
|
||||
if (this.sock != null)
|
||||
{
|
||||
if (this.sock.Error != null)
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.ERROR, "Exiting receive thread. Server: " + this.ServerAddress + " Error: " + this.sock.Error);
|
||||
this.HandleException(StatusCode.ExceptionOnConnect);
|
||||
}
|
||||
else
|
||||
{
|
||||
// connected
|
||||
if (this.ReportDebugOfLevel(DebugLevel.ALL))
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.ALL, "Receiving by websocket. this.State: " + this.State);
|
||||
}
|
||||
|
||||
this.State = PhotonSocketState.Connected;
|
||||
this.peerBase.OnConnect();
|
||||
|
||||
while (this.State == PhotonSocketState.Connected)
|
||||
{
|
||||
if (this.sock != null)
|
||||
{
|
||||
if (this.sock.Error != null)
|
||||
{
|
||||
this.Listener.DebugReturn(DebugLevel.ERROR, "Exiting receive thread (inside loop). Server: " + this.ServerAddress + " Error: " + this.sock.Error);
|
||||
this.HandleException(StatusCode.ExceptionOnReceive);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] inBuff = this.sock.Recv();
|
||||
if (inBuff == null || inBuff.Length == 0)
|
||||
{
|
||||
// nothing received. wait a bit, try again
|
||||
yield return new WaitForRealSeconds(0.02f);
|
||||
continue;
|
||||
}
|
||||
|
||||
//if (this.ReportDebugOfLevel(DebugLevel.ALL))
|
||||
//{
|
||||
// this.Listener.DebugReturn(DebugLevel.ALL, "TCP << " + inBuff.Length + " = " + SupportClassPun.ByteArrayToString(inBuff));
|
||||
//}
|
||||
|
||||
if (inBuff.Length > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.HandleReceivedDatagram(inBuff, inBuff.Length, false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (this.State != PhotonSocketState.Disconnecting && this.State != PhotonSocketState.Disconnected)
|
||||
{
|
||||
if (this.ReportDebugOfLevel(DebugLevel.ERROR))
|
||||
{
|
||||
this.EnqueueDebugReturn(DebugLevel.ERROR, "Receive issue. State: " + this.State + ". Server: '" + this.ServerAddress + "' Exception: " + e);
|
||||
}
|
||||
|
||||
this.HandleException(StatusCode.ExceptionOnReceive);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.Disconnect();
|
||||
}
|
||||
|
||||
|
||||
private class MonoBehaviourExt : MonoBehaviour
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
8
Assets/Photon/PhotonLibs/WebSocket/SocketWebTcp.cs.meta
Normal file
8
Assets/Photon/PhotonLibs/WebSocket/SocketWebTcp.cs.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac953d6a57a9ea94e96ec689598995d5
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
252
Assets/Photon/PhotonLibs/WebSocket/WebSocket.cs
Normal file
252
Assets/Photon/PhotonLibs/WebSocket/WebSocket.cs
Normal file
@@ -0,0 +1,252 @@
|
||||
#if UNITY_WEBGL || WEBSOCKET || WEBSOCKET_PROXYCONFIG
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <summary>
|
||||
// Provided originally by Unity to cover WebSocket support in WebGL and the Editor. Modified by Exit Games GmbH.
|
||||
// </summary>
|
||||
// <author>developer@exitgames.com</author>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace ExitGames.Client.Photon
|
||||
{
|
||||
using System;
|
||||
using System.Text;
|
||||
using ExitGames.Client.Photon;
|
||||
|
||||
#if UNITY_WEBGL && !UNITY_EDITOR
|
||||
using System.Runtime.InteropServices;
|
||||
#else
|
||||
using WebSocketSharp;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Authentication;
|
||||
#endif
|
||||
|
||||
|
||||
public class WebSocket
|
||||
{
|
||||
private Uri mUrl;
|
||||
private string mProxyAddress;
|
||||
|
||||
/// <summary>Photon uses this to agree on a serialization protocol. Either: GpBinaryV16 or GpBinaryV18. Based on enum SerializationProtocol.</summary>
|
||||
private string protocols = "GpBinaryV16";
|
||||
|
||||
public Action<DebugLevel, string> DebugReturn { get; set; }
|
||||
|
||||
public WebSocket(Uri url, string proxyAddress, string protocols = null)
|
||||
{
|
||||
this.mUrl = url;
|
||||
this.mProxyAddress = proxyAddress;
|
||||
|
||||
if (protocols != null)
|
||||
{
|
||||
this.protocols = protocols;
|
||||
}
|
||||
|
||||
string protocol = mUrl.Scheme;
|
||||
if (!protocol.Equals("ws") && !protocol.Equals("wss"))
|
||||
{
|
||||
throw new ArgumentException("Unsupported protocol: " + protocol);
|
||||
}
|
||||
}
|
||||
|
||||
public string ProxyAddress
|
||||
{
|
||||
get { return mProxyAddress; }
|
||||
}
|
||||
|
||||
public void SendString(string str)
|
||||
{
|
||||
Send(Encoding.UTF8.GetBytes(str));
|
||||
}
|
||||
|
||||
public string RecvString()
|
||||
{
|
||||
byte[] retval = Recv();
|
||||
if (retval == null)
|
||||
return null;
|
||||
return Encoding.UTF8.GetString(retval);
|
||||
}
|
||||
|
||||
#if UNITY_WEBGL && !UNITY_EDITOR
|
||||
[DllImport("__Internal")]
|
||||
private static extern int SocketCreate (string url, string protocols);
|
||||
|
||||
[DllImport("__Internal")]
|
||||
private static extern int SocketState (int socketInstance);
|
||||
|
||||
[DllImport("__Internal")]
|
||||
private static extern void SocketSend (int socketInstance, byte[] ptr, int length);
|
||||
|
||||
[DllImport("__Internal")]
|
||||
private static extern void SocketRecv (int socketInstance, byte[] ptr, int length);
|
||||
|
||||
[DllImport("__Internal")]
|
||||
private static extern int SocketRecvLength (int socketInstance);
|
||||
|
||||
[DllImport("__Internal")]
|
||||
private static extern void SocketClose (int socketInstance);
|
||||
|
||||
[DllImport("__Internal")]
|
||||
private static extern int SocketError (int socketInstance, byte[] ptr, int length);
|
||||
|
||||
int m_NativeRef = 0;
|
||||
|
||||
public void Send(byte[] buffer)
|
||||
{
|
||||
SocketSend (m_NativeRef, buffer, buffer.Length);
|
||||
}
|
||||
|
||||
public byte[] Recv()
|
||||
{
|
||||
int length = SocketRecvLength (m_NativeRef);
|
||||
if (length == 0)
|
||||
return null;
|
||||
byte[] buffer = new byte[length];
|
||||
SocketRecv (m_NativeRef, buffer, length);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
m_NativeRef = SocketCreate (mUrl.ToString(), this.protocols);
|
||||
|
||||
//while (SocketState(m_NativeRef) == 0)
|
||||
// yield return 0;
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
SocketClose(m_NativeRef);
|
||||
}
|
||||
|
||||
public bool Connected
|
||||
{
|
||||
get { return SocketState(m_NativeRef) != 0; }
|
||||
}
|
||||
|
||||
public string Error
|
||||
{
|
||||
get {
|
||||
const int bufsize = 1024;
|
||||
byte[] buffer = new byte[bufsize];
|
||||
int result = SocketError (m_NativeRef, buffer, bufsize);
|
||||
|
||||
if (result == 0)
|
||||
return null;
|
||||
|
||||
return Encoding.UTF8.GetString (buffer);
|
||||
}
|
||||
}
|
||||
#else
|
||||
WebSocketSharp.WebSocket m_Socket;
|
||||
Queue<byte[]> m_Messages = new Queue<byte[]>();
|
||||
bool m_IsConnected = false;
|
||||
string m_Error = null;
|
||||
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
m_Socket = new WebSocketSharp.WebSocket(mUrl.ToString(), new string[] {this.protocols});
|
||||
m_Socket.Log.Output = (ld, f) =>
|
||||
{
|
||||
var s = string.Format("WebSocketSharp: {0}", ld.Message);
|
||||
switch (ld.Level)
|
||||
{
|
||||
case WebSocketSharp.LogLevel.Trace:
|
||||
case WebSocketSharp.LogLevel.Debug:
|
||||
DebugReturn(DebugLevel.ALL, s);
|
||||
break;
|
||||
case WebSocketSharp.LogLevel.Info:
|
||||
DebugReturn(DebugLevel.INFO, s);
|
||||
break;
|
||||
case WebSocketSharp.LogLevel.Warn:
|
||||
DebugReturn(DebugLevel.WARNING, s);
|
||||
break;
|
||||
case WebSocketSharp.LogLevel.Error:
|
||||
case WebSocketSharp.LogLevel.Fatal:
|
||||
DebugReturn(DebugLevel.ERROR, s);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
string user = null;
|
||||
string pass = null;
|
||||
|
||||
if (!String.IsNullOrEmpty(mProxyAddress))
|
||||
{
|
||||
var authDelim = mProxyAddress.IndexOf("@");
|
||||
if (authDelim != -1)
|
||||
{
|
||||
user = mProxyAddress.Substring(0, authDelim);
|
||||
mProxyAddress = mProxyAddress.Substring(authDelim + 1);
|
||||
var passDelim = user.IndexOf(":");
|
||||
if (passDelim != -1)
|
||||
{
|
||||
pass = user.Substring(passDelim + 1);
|
||||
user = user.Substring(0, passDelim);
|
||||
}
|
||||
}
|
||||
|
||||
// throws an exception, if scheme not specified
|
||||
m_Socket.SetProxy("http://" + mProxyAddress, user, pass);
|
||||
}
|
||||
|
||||
if (m_Socket.IsSecure)
|
||||
{
|
||||
m_Socket.SslConfiguration.EnabledSslProtocols = m_Socket.SslConfiguration.EnabledSslProtocols | (SslProtocols)(3072 | 768);
|
||||
}
|
||||
|
||||
m_Socket.OnMessage += (sender, e) => { m_Messages.Enqueue(e.RawData); };
|
||||
m_Socket.OnOpen += (sender, e) => { m_IsConnected = true; };
|
||||
m_Socket.OnError += (sender, e) => { m_Error = e.Message + (e.Exception == null ? "" : " / " + e.Exception); };
|
||||
|
||||
this.m_Socket.OnClose += SocketOnClose;
|
||||
|
||||
m_Socket.ConnectAsync();
|
||||
}
|
||||
|
||||
private void SocketOnClose(object sender, CloseEventArgs e)
|
||||
{
|
||||
//UnityEngine.Debug.Log(e.Code.ToString());
|
||||
|
||||
// this code is used for cases when the socket failed to get created (specifically used to detect "blocked by Windows firewall")
|
||||
// for some reason this situation is not calling OnError
|
||||
if (e.Code == 1006)
|
||||
{
|
||||
this.m_Error = e.Reason;
|
||||
this.m_IsConnected = false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Connected
|
||||
{
|
||||
get { return m_IsConnected; }
|
||||
}
|
||||
|
||||
|
||||
public void Send(byte[] buffer)
|
||||
{
|
||||
m_Socket.Send(buffer);
|
||||
}
|
||||
|
||||
public byte[] Recv()
|
||||
{
|
||||
if (m_Messages.Count == 0)
|
||||
return null;
|
||||
return m_Messages.Dequeue();
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
m_Socket.Close();
|
||||
}
|
||||
|
||||
public string Error
|
||||
{
|
||||
get { return m_Error; }
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
8
Assets/Photon/PhotonLibs/WebSocket/WebSocket.cs.meta
Normal file
8
Assets/Photon/PhotonLibs/WebSocket/WebSocket.cs.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b1bad04f7805f764dba77f0d4518e0f0
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
116
Assets/Photon/PhotonLibs/WebSocket/WebSocket.jslib
Normal file
116
Assets/Photon/PhotonLibs/WebSocket/WebSocket.jslib
Normal file
@@ -0,0 +1,116 @@
|
||||
var LibraryWebSockets = {
|
||||
$webSocketInstances: [],
|
||||
|
||||
SocketCreate: function(url, protocols)
|
||||
{
|
||||
var str = UTF8ToString(url);
|
||||
var prot = UTF8ToString(protocols);
|
||||
var socket = {
|
||||
socket: new WebSocket(str, [prot]),
|
||||
buffer: new Uint8Array(0),
|
||||
error: null,
|
||||
messages: []
|
||||
}
|
||||
socket.socket.binaryType = 'arraybuffer';
|
||||
socket.socket.onmessage = function (e) {
|
||||
// if (e.data instanceof Blob)
|
||||
// {
|
||||
// var reader = new FileReader();
|
||||
// reader.addEventListener("loadend", function() {
|
||||
// var array = new Uint8Array(reader.result);
|
||||
// socket.messages.push(array);
|
||||
// });
|
||||
// reader.readAsArrayBuffer(e.data);
|
||||
// }
|
||||
if (e.data instanceof ArrayBuffer)
|
||||
{
|
||||
var array = new Uint8Array(e.data);
|
||||
socket.messages.push(array);
|
||||
}
|
||||
};
|
||||
socket.socket.onclose = function (e) {
|
||||
if (e.code != 1000)
|
||||
{
|
||||
if (e.reason != null && e.reason.length > 0)
|
||||
socket.error = e.reason;
|
||||
else
|
||||
{
|
||||
switch (e.code)
|
||||
{
|
||||
case 1001:
|
||||
socket.error = "Endpoint going away.";
|
||||
break;
|
||||
case 1002:
|
||||
socket.error = "Protocol error.";
|
||||
break;
|
||||
case 1003:
|
||||
socket.error = "Unsupported message.";
|
||||
break;
|
||||
case 1005:
|
||||
socket.error = "No status.";
|
||||
break;
|
||||
case 1006:
|
||||
socket.error = "Abnormal disconnection.";
|
||||
break;
|
||||
case 1009:
|
||||
socket.error = "Data frame too large.";
|
||||
break;
|
||||
default:
|
||||
socket.error = "Error "+e.code;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var instance = webSocketInstances.push(socket) - 1;
|
||||
return instance;
|
||||
},
|
||||
|
||||
SocketState: function (socketInstance)
|
||||
{
|
||||
var socket = webSocketInstances[socketInstance];
|
||||
return socket.socket.readyState;
|
||||
},
|
||||
|
||||
SocketError: function (socketInstance, ptr, bufsize)
|
||||
{
|
||||
var socket = webSocketInstances[socketInstance];
|
||||
if (socket.error == null)
|
||||
return 0;
|
||||
stringToUTF8(socket.error, ptr, bufsize);
|
||||
return 1;
|
||||
},
|
||||
|
||||
SocketSend: function (socketInstance, ptr, length)
|
||||
{
|
||||
var socket = webSocketInstances[socketInstance];
|
||||
socket.socket.send (HEAPU8.buffer.slice(ptr, ptr+length));
|
||||
},
|
||||
|
||||
SocketRecvLength: function(socketInstance)
|
||||
{
|
||||
var socket = webSocketInstances[socketInstance];
|
||||
if (socket.messages.length == 0)
|
||||
return 0;
|
||||
return socket.messages[0].length;
|
||||
},
|
||||
|
||||
SocketRecv: function (socketInstance, ptr, length)
|
||||
{
|
||||
var socket = webSocketInstances[socketInstance];
|
||||
if (socket.messages.length == 0)
|
||||
return 0;
|
||||
if (socket.messages[0].length > length)
|
||||
return 0;
|
||||
HEAPU8.set(socket.messages[0], ptr);
|
||||
socket.messages = socket.messages.slice(1);
|
||||
},
|
||||
|
||||
SocketClose: function (socketInstance)
|
||||
{
|
||||
var socket = webSocketInstances[socketInstance];
|
||||
socket.socket.close();
|
||||
}
|
||||
};
|
||||
|
||||
autoAddDeps(LibraryWebSockets, '$webSocketInstances');
|
||||
mergeInto(LibraryManager.library, LibraryWebSockets);
|
||||
21
Assets/Photon/PhotonLibs/WebSocket/WebSocket.jslib.meta
Normal file
21
Assets/Photon/PhotonLibs/WebSocket/WebSocket.jslib.meta
Normal file
@@ -0,0 +1,21 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 04bb5f307f2e48b4fbaa6da865fd4091
|
||||
PluginImporter:
|
||||
serializedVersion: 1
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
isPreloaded: 0
|
||||
platformData:
|
||||
Any:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
Editor:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
WebGL:
|
||||
enabled: 1
|
||||
settings: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
UPDATED: WebSocket/websocket-sharp.dll from our own Git Fork. Commit: 77f74bd
|
||||
forked from https://github.com/sta/websocket-sharp.git
|
||||
|
||||
websocket-sharp is provided under The MIT License as mentioned here: https://github.com/sta/websocket-sharp#license
|
||||
@@ -0,0 +1,4 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3e262c2b04eaa8440987b50a91e86674
|
||||
DefaultImporter:
|
||||
userData:
|
||||
BIN
Assets/Photon/PhotonLibs/WebSocket/websocket-sharp.dll
Normal file
BIN
Assets/Photon/PhotonLibs/WebSocket/websocket-sharp.dll
Normal file
Binary file not shown.
137
Assets/Photon/PhotonLibs/WebSocket/websocket-sharp.dll.meta
Normal file
137
Assets/Photon/PhotonLibs/WebSocket/websocket-sharp.dll.meta
Normal file
@@ -0,0 +1,137 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 748eb70bc0d7515498ef73fed155520a
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
platformData:
|
||||
- first:
|
||||
'': Any
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
Exclude Android: 0
|
||||
Exclude Editor: 0
|
||||
Exclude Linux: 0
|
||||
Exclude Linux64: 0
|
||||
Exclude LinuxUniversal: 0
|
||||
Exclude OSXUniversal: 0
|
||||
Exclude WebGL: 1
|
||||
Exclude Win: 0
|
||||
Exclude Win64: 0
|
||||
Exclude WindowsStoreApps: 1
|
||||
- first:
|
||||
'': OSXIntel
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
- first:
|
||||
'': OSXIntel64
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
- first:
|
||||
'': WP8
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
DontProcess: False
|
||||
PlaceholderPath:
|
||||
- first:
|
||||
Android: Android
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
CPU: ARMv7
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 1
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
DefaultValueInitialized: true
|
||||
OS: AnyOS
|
||||
- first:
|
||||
Facebook: Win
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
- first:
|
||||
Facebook: Win64
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
- first:
|
||||
Standalone: Linux
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
CPU: x86
|
||||
- first:
|
||||
Standalone: Linux64
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
CPU: x86_64
|
||||
- first:
|
||||
Standalone: LinuxUniversal
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
- first:
|
||||
Standalone: OSXUniversal
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
- first:
|
||||
Standalone: Win
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
- first:
|
||||
Standalone: Win64
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
- first:
|
||||
Windows Store Apps: WindowsStoreApps
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
DontProcess: False
|
||||
PlaceholderPath:
|
||||
SDK: AnySDK
|
||||
ScriptingBackend: AnyScriptingBackend
|
||||
- first:
|
||||
XboxOne: XboxOne
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
CompileFlags:
|
||||
FrameworkDependencies:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user