Ejemplo n.º 1
0
        public async Task Start()
        {
            var buffer = new byte[1024 * 4];
            WebSocketReceiveResult result = await this.Socket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);

            this.IsWorking = true;

            while (!result.CloseStatus.HasValue)
            {
                //个人觉得实际上可以将下面的webSocket.SendAsync方法放到一个Task里面,来启动另一个线程然后在内部不断循环来处理发送数据的任务,避免和下面的webSocket.ReceiveAsync轮流执行,从而提高效率

                string res = System.Text.Encoding.UTF8.GetString(buffer).Trim('\0');
                try {
                    using (dpz.Jsons.Jttp jttp = dpz.Jsons.Jttp.Parse(res)) {
                        if (jttp.Header.Type == "")
                        {
                            await SendError(result, "未发现交互类型"); break;
                        }
                        else
                        {
                            await this.Processor.Run(result, this, jttp);
                        }
                    }
                } catch (Exception ex) {
                    await SendError(result, "未知错误:" + ex.Message);
                }
                //byte[] bs = System.Text.Encoding.UTF8.GetBytes("收到数据:" + res);

                result = await this.Socket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);
            }
            await this.Socket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
        }
Ejemplo n.º 2
0
        public string Get()
        {
            dpz.Jsons.Jttp res = new dpz.Jsons.Jttp();

            res.Header.Time = "" + dpz.Time.Now.ToTimeStamp();

            //创建控制器类型列表
            List <Type> controllerTypes = new List <Type>();

            //加载程序集
            var assembly = Assembly.GetExecutingAssembly();

            //获取程序集下所有的类,通过Linq筛选继承IController类的所有类型
            controllerTypes.AddRange(assembly.GetTypes().Where(type => typeof(ControllerBase).IsAssignableFrom(type)));

            res.Data.Apis = new List <dpz.Dynamic>();

            //遍历控制器类
            foreach (var controller in controllerTypes)
            {
                //dynamic dc = new dpz.Dynamic();

                string clrName = controller.Name;
                if (clrName.EndsWith("Controller"))
                {
                    clrName = clrName.Substring(0, controller.Name.Length - 10);
                }
                string clrPath = "/" + controller.Name.Substring(0, controller.Name.Length - 10);
                var    actions = controller.GetMethods().Where(method => method.IsPublic == true);

                var route = controller.GetCustomAttribute(typeof(RouteAttribute)) as RouteAttribute;
                if (route != null)
                {
                    clrPath = "/" + route.Template.Replace("[controller]", clrName);
                    //dc.Route = route.Name;
                }

                foreach (var action in actions)
                {
                    var yapi = action.GetCustomAttribute(typeof(YapiAttribute)) as YapiAttribute;

                    if (yapi != null)
                    {
                        dynamic dc = new dpz.Dynamic();
                        res.Data.Apis.Add(dc);

                        dc.Url         = clrPath + "/" + action.Name;
                        dc.LoginNeed   = yapi.LoginNeed;
                        dc.Controller  = controller.FullName;
                        dc.Description = yapi.Description;
                    }
                }
            }
            return(res.ToJson());

            //return obj.ToJson();
        }
Ejemplo n.º 3
0
        public string Get()
        {
            dpz.Jsons.Jttp res = new dpz.Jsons.Jttp();

            //FillKernel(res.Data);

            //FillUpdate(res.Data);

            FillEnvironment(res.Data);
            res.Data.Environment.RootPath = host.ContentRootPath;

            return(res.ToJson());
        }
Ejemplo n.º 4
0
        //获取GUID
        private async Task SendError(WebSocketReceiveResult result, string msg, int code = 0, string tp = "")
        {
            using (dpz.Jsons.Jttp jttp = new dpz.Jsons.Jttp()) {
                jttp.Header.Ver    = "1.0";
                jttp.Header.Type   = tp;
                jttp.Header.Time   = "" + dpz.Time.Now.ToTimeStamp();
                jttp.Header.Status = "-1";
                jttp.Header.Error  = "" + code;
                jttp.Message       = msg;

                byte[] bs = System.Text.Encoding.UTF8.GetBytes(jttp.ToJson());
                await this.Socket.SendAsync(new ArraySegment <byte>(bs, 0, bs.Length), result.MessageType, result.EndOfMessage, CancellationToken.None);
            }
        }
Ejemplo n.º 5
0
        public string CreateNew()
        {
            dpz.Jsons.Jttp res = new dpz.Jsons.Jttp();

            res.Header.Time = "" + dpz.Time.Now.ToTimeStamp();

            using (dpz.Mvc.Sessions.RedisSessionManager redis = new dpz.Mvc.Sessions.RedisSessionManager(site.Config.Redis.ConnectionString, false)) {
                redis.CreateSessionId();

                string time = "" + dpz.Time.Now.ToTimeStamp();

                redis["Session_Time"] = time;

                res.Data.Sid  = redis.SessionID;
                res.Data.Time = time;
            }

            res.Header.Status = "1";
            return(res.ToJson());

            //return obj.ToJson();
        }
Ejemplo n.º 6
0
        public string GetConfig()
        {
            dpz.Jsons.Jttp res = new dpz.Jsons.Jttp();

            res.Header.Time = "" + dpz.Time.Now.ToTimeStamp();

            string ip = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
                        .Select(p => p.GetIPProperties())
                        .SelectMany(p => p.UnicastAddresses)
                        .Where(p => p.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(p.Address))
                        .FirstOrDefault()?.Address.ToString();
            //res.Data.Sid = redis.SessionID;
            //res.Data.Time = time;
            var websocket = new dpz.Dynamic();

            res.Data.websocket = websocket;
            websocket["url"]   = site.Config.Url.Websocket.Replace("${IP}", ip);

            res.Header.Status = "1";
            return(res.ToJson());

            //return obj.ToJson();
        }
Ejemplo n.º 7
0
        private string GetInfoBySid(string sid, string randString, string md5)
        {
            dpz.Jsons.Jttp res = new dpz.Jsons.Jttp();

            if (sid.IsNone())
            {
                res.Header.Status = "0";
                res.Message       = "交互标识无效";
                return(res.ToJson());
            }

            if (randString.IsNone())
            {
                res.Header.Status = "0";
                res.Message       = "缺少身份授权所需的随机字符串";
                return(res.ToJson());
            }

            if (randString.Length < 32)
            {
                res.Header.Status = "0";
                res.Message       = "为保证通讯安全,身份授权所需的随机字符串最少长度为32位";
                return(res.ToJson());
            }

            if (md5.IsNone())
            {
                res.Header.Status = "0";
                res.Message       = "缺少身份授权所需的验证码";
                return(res.ToJson());
            }

            using (dpz.Mvc.Sessions.RedisSessionManager redis = new dpz.Mvc.Sessions.RedisSessionManager(site.Config.Redis.ConnectionString, false, sid)) {
                //redis.CreateSessionId();
                if (!redis.CheckSessionId(sid, false))
                {
                    res.Header.Status = "0";
                    res.Message       = "交互标识无效";
                    return(res.ToJson());
                }

                long   uid        = redis["User_ID"].ToLong();
                string sessionKey = redis["Session_Key"];

                if (uid <= 0)
                {
                    res.Header.Status = "0";
                    res.Message       = "用户尚未登录或登陆状态失效";
                    return(res.ToJson());
                }

                using (dpz.Gdbc.Connection dbc = new dpz.Gdbc.Connection(site.Config.Database.Aos)) {
                    var row = dbc.GetGdmlOne($"@{{$[AosUsers]&[ID=='{uid}']}}");
                    if (row.IsEmpty)
                    {
                        res.Header.Status = "0";
                        res.Message       = "未找到用户信息";
                        return(res.ToJson());
                    }

                    string name    = row["Name"];
                    string userMD5 = ("name=" + name + "&str=" + randString + "&key=" + sessionKey).GetMD5();
                    if (md5 != userMD5)
                    {
                        res.Header.Status = "0";
                        res.Message       = "MD5验证失败";
                        return(res.ToJson());
                    }

                    foreach (var item in row)
                    {
                        if (item.Key != "Pwd")
                        {
                            res.Data[item.Key] = item.Value;
                        }
                    }
                }

                res.Header.Status = "1";
                return(res.ToJson());
            }
        }
Ejemplo n.º 8
0
 public string Debug()
 {
     dpz.Jsons.Jttp res = new dpz.Jsons.Jttp();
     FillDebug(res.Data);
     return(res.ToJson());
 }
Ejemplo n.º 9
0
 public string Environment()
 {
     dpz.Jsons.Jttp res = new dpz.Jsons.Jttp();
     FillEnvironment(res.Data);
     return(res.ToJson());
 }
Ejemplo n.º 10
0
 public string Update()
 {
     dpz.Jsons.Jttp res = new dpz.Jsons.Jttp();
     FillUpdate(res.Data);
     return(res.ToJson());
 }