Exemple #1
0
        static IEnumerator FetchBytes(WWW www, IObserver<byte[]> observer, IProgress<float> reportProgress, CancellationToken cancel)
        {
            using (www)
            {
                while (!www.isDone && !cancel.IsCancellationRequested)
                {
                    if (reportProgress != null)
                    {
                        try
                        {
                            reportProgress.Report(www.progress);
                        }
                        catch (Exception ex)
                        {
                            observer.OnError(ex);
                            yield break;
                        }
                    }
                    yield return null;
                }

                if (cancel.IsCancellationRequested) yield break;

                if (!string.IsNullOrEmpty(www.error))
                {
                    observer.OnError(new Exception(www.error));
                }
                else
                {
                    observer.OnNext(www.bytes);
                    observer.OnCompleted();
                }
            }
        }
        static IEnumerator AsObservableCore(AsyncOperation asyncOperation, IObserver<AsyncOperation> observer, IProgress<float> reportProgress, CancellationToken cancel)
        {
            while (!asyncOperation.isDone && !cancel.IsCancellationRequested)
            {
                if (reportProgress != null)
                {
                    try
                    {
                        reportProgress.Report(asyncOperation.progress);
                    }
                    catch (Exception ex)
                    {
                        observer.OnError(ex);
                        yield break;
                    }
                }
                yield return null;
            }

            if (cancel.IsCancellationRequested) yield break;

            observer.OnNext(asyncOperation);
            observer.OnCompleted();
        }
Exemple #3
0
        /// <summary>
        /// The core method which run the async connection process.
        /// </summary>
        /// <param name="observer"> The return value of InitAsObservable() method </param>
        /// <param name="cancelToken"> The cancel token object which signals to stop the currnet coroutine </param>
        /// <returns></returns>
        IEnumerator InitCore(UniRx.IObserver <Socket> observer, UniRx.CancellationToken cancelToken)
        {
            // Declare to connect in socket.io v1.0
            _urlQueries.Add("EIO", "3");
            _urlQueries.Add("transport", "polling");
            _urlQueries.Add("t", TimeStamp.Now);

            // Try get WebSocketTrigger instance if a connection already established _baseUrl.
            var webSocketTrigger = SocketManager.Instance.GetWebSocketTrigger(BaseUrl);

            if (webSocketTrigger == null || !webSocketTrigger.IsConnected)
            {
                var www = new WWW(PollingUrl);

                while (!www.isDone && !cancelToken.IsCancellationRequested)
                {
                    yield return(null);
                }

                if (cancelToken.IsCancellationRequested)
                {
                    yield break;
                }

                if (!string.IsNullOrEmpty(www.error))
                {
                    observer.OnError(new Exception(www.error));
                    yield break;
                }

                var textIndex    = www.text.IndexOf('{');
                var textEndIndex = www.text.IndexOf('}');
                var json         = www.text.Substring(textIndex, textEndIndex - (textIndex - 1));

                Debug.LogFormat("json: {0}", json);


                if (!string.IsNullOrEmpty(json))
                {
                    var answer = JsonConvert.DeserializeObject <PollingUrlAnswer>(json);
                    //var answer = JsonUtility.FromJson<PollingUrlAnswer>(json);

                    Debug.LogFormat("{0}", answer);

                    _urlQueries.Add("sid", answer.sid);
                }

                if (webSocketTrigger == null)
                {
                    webSocketTrigger = new GameObject(string.Format("WebSocket - {0}", BaseUrl)).AddComponent <WebSocketTrigger>();
                    SocketManager.Instance.RegisterWebSocketTrigger(BaseUrl, webSocketTrigger);
                }
            }

            _urlQueries["transport"] = "websocket";
            _urlQueries.Remove("t");

            webSocketTrigger.WebSocket = new WebSocketWrapper(new Uri(WebSocketUrl));
            Socket.transform.parent    = webSocketTrigger.transform;

            webSocketTrigger.WebSocket.Connect();
            yield return(new WaitUntil(() => webSocketTrigger.IsConnected));

            if (cancelToken.IsCancellationRequested)
            {
                webSocketTrigger.WebSocket.Close();
                yield break;
            }

            webSocketTrigger.WebSocket.Send(Packet.Probe);
            yield return(new WaitUntil(() => webSocketTrigger.IsProbed));

            if (cancelToken.IsCancellationRequested)
            {
                webSocketTrigger.WebSocket.Close();
                yield break;
            }

            webSocketTrigger.WebSocket.Send(new Packet(EnginePacketTypes.UPGRADE).Encode());
            webSocketTrigger.IsUpgraded = true;

            // Try to activate Socket.io namespace
            if (Socket.HasNamespace)
            {
                webSocketTrigger.WebSocket.Send(new Packet(EnginePacketTypes.MESSAGE, SocketPacketTypes.CONNECT, Socket.Namespace, string.Empty).Encode());
            }

            var capturedSocket = Socket;

            // Start to receive a incoming WebSocket packet
            webSocketTrigger.OnRecvAsObservable()
            .Subscribe(r => { capturedSocket.OnRecvWebSocketPacket(r); })
            .AddTo(Socket);

            observer.OnNext(Socket);

            yield return(new WaitForSeconds(1f));

            observer.OnCompleted();
        }
Exemple #4
0
        static IEnumerator FetchAssetBundle(WWW www, IObserver<AssetBundle> observer, IProgress<float> reportProgress, CancellationToken cancel)
        {
            using (www)
            {
                if (reportProgress != null)
                {
                    while (!www.isDone && !cancel.IsCancellationRequested)
                    {
                        try
                        {
                            reportProgress.Report(www.progress);
                        }
                        catch (Exception ex)
                        {
                            observer.OnError(ex);
                            yield break;
                        }
                        yield return null;
                    }
                }
                else
                {
                    if (!www.isDone)
                    {
                        yield return www;
                    }
                }

                if (cancel.IsCancellationRequested)
                {
                    if (!www.isDone) yield return www; // workaround for freeze bug of dispose WWW when WWW is not completed
                    yield break;
                }

                if (reportProgress != null)
                {
                    try
                    {
                        reportProgress.Report(www.progress);
                    }
                    catch (Exception ex)
                    {
                        observer.OnError(ex);
                        yield break;
                    }
                }

                if (!string.IsNullOrEmpty(www.error))
                {
                    observer.OnError(new WWWErrorException(www));
                }
                else
                {
                    observer.OnNext(www.assetBundle);
                    observer.OnCompleted();
                }
            }
        }