Exemple #1
0
    internal void FunctionA()
    {
        Task task = new Task(() =>
        {
            string _name = "CreateWithTimer ←";
            Timer.AddTimer(0).OnCompleted(() =>
            {
                text.text = _name;  //先演示异常
                new GameObject(_name);
            });
        });

        task.Start();
    }
Exemple #2
0
 void Awake()
 {
     savePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TestDownLoad");
     Timer.IntializeDriver();
     Loom.Initialize();
     DownLoader = new HttpDownLoader();
     DownLoader.OnDownLoadCompleted.AddListener(() =>
     {
         this.finish.text = "下载完成!";
     });
     DownLoader.OnDownLoadUpdate.AddListener(v =>
     {
         this.update.text = string.Format("下载进度:{0} %", (v * 100).ToString("f2"));
     });
 }
Exemple #3
0
    } = false;                                        //表示下载是否完成


    /// <summary>
    /// 下载方法(断点续传)
    /// </summary>
    /// <param name="url">URL下载地址</param>
    /// <param name="savePath">Save path保存路径</param>
    /// <param name="callBack">Call back回调函数</param>
    public void DownLoad(string url, string savePath)
    {
        isStop        = false;
        Timer.ShowLog = false;
        ThreadStart DownLoadHandler = () =>
        {
            //判断保存路径是否存在
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            //这是要下载的文件名,比如从服务器下载a.zip到D盘,保存的文件名是test
            string filePath = savePath + "/test";
            float  progress = 0;

            //使用流操作文件
            FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
            //获取文件现在的长度
            long fileLength = fs.Length;
            //获取下载文件的总长度
            long totalLength = GetLength(url);

            //如果没下载完
            if (fileLength < totalLength)
            {
                //断点续传核心,设置本地文件流的起始位置
                fs.Seek(fileLength, SeekOrigin.Begin);

                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

                //断点续传核心,设置远程访问文件流的起始位置
                request.AddRange((int)fileLength);
                Stream stream = request.GetResponse().GetResponseStream();

                byte[] buffer = new byte[1024];
                //使用流读取内容到buffer中
                //注意方法返回值代表读取的实际长度,并不是buffer有多大,stream就会读进去多少
                int length = stream.Read(buffer, 0, buffer.Length);
                while (length > 0)
                {
                    //如果Unity客户端关闭,停止下载
                    if (isStop)
                    {
                        break;
                    }
                    //将内容再写入本地文件中
                    fs.Write(buffer, 0, length);
                    //计算进度
                    fileLength += length;
                    progress    = (float)fileLength / (float)totalLength;
                    //Loom.QueueOnMainThread((v) =>
                    //{
                    //    OnDownLoadUpdate?.Invoke(progress);
                    //},null);
                    Timer.AddTimer(0, "ForUpdateEvent", true).OnCompleted(() =>
                    {
                        OnDownLoadUpdate?.Invoke(progress);
                    });
                    //类似尾递归
                    length = stream.Read(buffer, 0, buffer.Length);
                }
                stream.Close();
                stream.Dispose();
            }
            else
            {
                progress = 1;
            }
            fs.Close();
            fs.Dispose();
            //如果下载完毕,执行回调
            if (progress == 1)
            {
                isDone = true;
                Timer.DelTimer("ForUpdateEvent");
                Timer.AddTimer(0, "ForOnCompleteEvent").OnCompleted(() =>
                {
                    OnDownLoadCompleted?.Invoke();
                });
            }
        };

        thread = new Thread(DownLoadHandler); //开启子线程下载,使用匿名方法
        thread.IsBackground = true;           //开启子线程
        thread.Start();
    }
Exemple #4
0
    private void ResponseThread(object obj)
    {
        var context = (HttpListenerContext)obj;

        //		Debug.Log("request for " + context.Request.Url.AbsolutePath);
        var res = context.Response;

        res.StatusCode = 200;
        var output = new StreamWriter(res.OutputStream);
        var path   = context.Request.Url.AbsolutePath;

        Timer.AddTimer(0).OnCompleted(() =>
        {
            text.text = path;
        });

        switch (path)
        {
        case "/bigFile":
        {
            var  str   = "Lorem ipsum dolor sit amet.\n";
            long count = 1024 * 1024 * 100;

            res.AddHeader("Content-length", (str.Length * count).ToString());
            res.AddHeader("Content-type", "application/octet-stream");

            //For speed, prep a buffer to bulk move from.
            var strBytes = Encoding.ASCII.GetBytes(str);
            var buf      = new byte[1024 * strBytes.Length];
            for (int i = 0; i < 1024; i++)
            {
                Array.Copy(strBytes, 0, buf, i * strBytes.Length, strBytes.Length);
            }

            //Send data
            for (int i = 0; i < count / 1024; i++)
            {
                res.OutputStream.Write(buf, 0, buf.Length);
            }
            break;
        }

        case "/slowFile":

        case "/slowPage":
        {
            var str   = "Lorem ipsum dolor sit amet.\n";
            var count = 1024 * 1024;

            res.AddHeader("Content-length", (str.Length * count).ToString());
            res.AddHeader("Content-type", path == "/slowFile" ? "application/octet-stream" : "text/plain");

            for (int i = 0; i < count; i++)
            {
                output.Write(str);
                Thread.Sleep(1);
            }
            break;
        }

        default:
            context.Response.StatusCode = 404;
            output.Write("Not found");
            break;
        }

        output.Close();
    }
Exemple #5
0
 private void Awake()
 {
     Timer.IntializeDriver();
 }
Exemple #6
0
 private void Awake()
 {
     Timer.IntializeDriver(); //首次初始化不能放在非主线程内。
     Loom.Initialize();       //首次初始化不能放在非主线程内。
 }