示例#1
0
        void _OnClose(int code = 0)
        {
            this._CancelHeartBeat();

            if (this.m_CloseRequired)
            {
                this.m_ReadyState = GatewayState.Init;
                return;
            }

            if (this.m_Socket != null)
            {
                //https://github.com/sta/websocket-sharp/issues/219
                if (code == 1015 && this.m_Socket.checkSslProtocolHackFlag())
                {
                    this.m_SslHandShakeError = true;
                }

                this.m_Socket.Close();
                this.m_Socket = null;
            }

            this._Reset(isInit: false);

            bool reset = !this.resumable || this.m_BackOff.fail > 0;

            if (code != 0)
            {
                this.m_CandidateIndex++;
            }
            var delay = this._OnFail(reset);

            NetworkStatusManager.isConnected = false;
            DebugerUtils.DebugAssert(false, $"connection failed, retry in {delay / 1000f} seconds");
        }
示例#2
0
        public void Send(string content)
        {
            DebugerUtils.DebugAssert(this.m_State == WebSocketState.Connected,
                                     "fatal error: Cannot send data before connect!");
            DebugerUtils.DebugAssert(this.m_Socket != null,
                                     "fatal error: Cannot send data because the websocket is null.");

            if (!this.connected)
            {
                return;
            }

            var bytes = Encoding.UTF8.GetBytes(content);

            this.m_Socket.Send(bytes);
        }
示例#3
0
        void Start()
        {
            if (_singletonChecker)
            {
                DebugerUtils.DebugAssert(false, "fatal error! Cannot initialize two WebSocketHost!");
                return;
            }

            _singletonChecker = true;

            this._socketGateway = new SocketGateway(this);

            this.m_InternetState = NetworkReachability.NotReachable;

            SocketApi.ResetState();
        }
        public string GetCachedFilePath(string url)
        {
            var ret = this.m_Connection.Table <FileRecordLite>().Where(record => record.url == url);

            if (!ret.Any())
            {
                return(null);
            }

            if (ret.Count() == 1)
            {
                return(ret.First().filepath);
            }

            DebugerUtils.DebugAssert(false, "fatal error: duplicated files are mapping to one url.");
            return(null);
        }
        public DBReadyStateLite LoadReadyState()
        {
            var ret = this.m_Connection.Table <DBReadyStateLite>()
                      .Where(record => record.key == DBReadyStateLite.DefaultReadyStateKey);

            if (!ret.Any())
            {
                return(null);
            }

            if (ret.Count() == 1)
            {
                return(ret.First());
            }

            DebugerUtils.DebugAssert(false, "fatal error: duplicated ready states are mapping to the default key.");
            return(null);
        }
示例#6
0
        public void Connect(string url, Action OnConnected, Action <byte[]> OnMessage,
                            Action <string> OnError, Action <int> OnClose)
        {
            DebugerUtils.DebugAssert(this.m_State == WebSocketState.NotConnected,
                                     $"fatal error: Cannot Connect to {url} because the socket is already set up.");
            this.m_State = WebSocketState.Connecting;

            this.m_Socket = new WebSocketSharp.WebSocket(url);
            if (this.m_useSslHandShakeHack)
            {
                this.m_Socket.SslConfiguration.EnabledSslProtocols = sslProtocolHack;
            }

//            if (EnableWebSocketSharpLog) {
//                this.m_Socket.Log.Level = LogLevel.Debug;
//                this.m_Socket.Log.File = @"log.txt";
//            }

            this.m_Socket.OnOpen += (sender, e) => {
                this.m_State = WebSocketState.Connected;
                this.m_Host.Enqueue(() => { OnConnected?.Invoke(); });
            };

            this.m_Socket.OnMessage += (sender, e) => { this.m_Host.Enqueue(() => { OnMessage(e.RawData); }); };

            this.m_Socket.OnError += (sender, e) => {
                this.m_State = WebSocketState.Error;
                this.m_Host.Enqueue(() => {
                    OnError(e.Message);
                });
            };

            this.m_Socket.OnClose += (sender, e) => {
                this.m_State = WebSocketState.Closed;
                this.m_Host.Enqueue(() => {
                    OnClose?.Invoke(e.Code);
                });
            };

            //disable logs
            this.m_Socket.Log.Output = (data, s) => { };

            this.m_Socket.ConnectAsync();
        }
示例#7
0
        public SocketGateway(WebSocketHost host)
        {
            if (m_Instance != null)
            {
                DebugerUtils.DebugAssert(false, "fatal error: duplicated socket gateways is not allowed!");
                return;
            }

            m_Instance  = this;
            this.m_Host = host;

            this.m_CandidateURLs  = new List <string>();
            this.m_CandidateIndex = 0;
            this.m_PayloadQueue   = new List <string>();
            this.m_BackOff        = new BackOff(host, 1000, 30000);
            this.m_CommitId       = null;
            this.m_Socket         = null;

            this._Reset(isInit: true);
        }
示例#8
0
        public static string SuitableSizeImageUrl(float imageWidth, string imageUrl)
        {
            var devicePixelRatio = Window.instance.devicePixelRatio;

            if (imageWidth <= 0)
            {
                DebugerUtils.DebugAssert(imageWidth <= 0, $"Image width error, width: {imageWidth}");
            }

            var networkImageWidth = Math.Ceiling(imageWidth * devicePixelRatio);

            if (networkImageWidth <= ImageWidthMin)
            {
                networkImageWidth = ImageWidthMin;
            }
            else if (networkImageWidth >= ImageWidthMax)
            {
                networkImageWidth = ImageWidthMax;
            }

            var url = $"{imageUrl}.{networkImageWidth}x0x1.jpg";

            return(url);
        }
示例#9
0
        public void Connect(Action <string> onIdentify, Action <string, SocketResponseDataBase> onMessage,
                            bool reconnect = false)
        {
            if (this.m_ReadyState != GatewayState.CLOSED && this.m_ReadyState != GatewayState.Init)
            {
                return;
            }

            if (!reconnect)
            {
                this.m_OnIdentify    = onIdentify;
                this.m_OnMessage     = onMessage;
                this.m_CloseRequired = false;
            }
            else
            {
                DebugerUtils.DebugAssert(this.m_OnIdentify != null, "fatal error: reconnect before initial connect !");
                DebugerUtils.DebugAssert(this.m_OnMessage != null, "fatal error: reconnect before initial connect !");
            }

            this.m_ReadyState = GatewayState.CONNECTING;
            this.m_BackOff.Cancel();

            this._SelectGateway(
                createWebSocketFunc:
                url => {
                if (url != null)
                {
                    this._Reset(isInit: true);
                    this.m_Socket?.Close();
                    this.m_Socket = null;

                    this.m_Socket = new WebSocket(this.m_Host, this.m_SslHandShakeError);
                    this.m_Socket.Connect(url,
                                          OnError: msg => { this._OnClose(1); },
                                          OnClose: this._OnClose,
                                          OnConnected: () => {
                        NetworkStatusManager.isConnected = true;
                        DebugerUtils.DebugAssert(this.m_CommitId != null,
                                                 "fatal error: commit Id is not correctly set before connection!");

                        this.m_ReadyState = GatewayState.OPEN;
                        if (!this._Resume())
                        {
                            this.m_OnIdentify.Invoke(this.m_CommitId);
                        }

                        this._StartHeartBeat();
                    },
                                          OnMessage: bytes => {
                        NetworkStatusManager.isConnected = true;
                        string type = "";
                        var data    = this._OnMessage(bytes, ref type);
                        if (data != null)
                        {
                            this.m_OnMessage?.Invoke(type, data);
                        }
                    }
                                          );
                }
                else
                {
                    NetworkStatusManager.isConnected = false;
                    var delay = this._OnFail();
                    DebugerUtils.DebugAssert(false, $"gateway discovery failed, retry in {delay / 1000f} seconds");
                }
            });
        }