コード例 #1
0
        private void Run()
        {
            FleckLog.Level = LogLevel.Debug;
            var allSockets = new List <IWebSocketConnection>();
            var server     = new WebSocketServer($"{sockettype }://{ipaddres}:{ ipport}//");

            try
            {
                Action <IWebSocketConnection> config = new Action <IWebSocketConnection>(socket =>
                {
                    socket.OnOpen = () =>
                    {
                        allSockets.Add(socket);
                    };
                    socket.OnClose = () =>
                    {
                        Console.WriteLine();
                        allSockets.Remove(socket);
                    };
                    socket.OnMessage = message =>
                    {
                        Markmessage = message;
                        Console.WriteLine(Markmessage);
                        //message 然后开启查询
                        allSockets.ToList().ForEach(s => s.Send($"Echo: {message}{socket.ConnectionInfo.Path}"));
                    };
                });
                WeeklyAction weeklyAction = new WeeklyAction(SendMsg);
                weeklyAction.Invoke(server, config);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"错误:  { ex.Message}");
            }
        }
コード例 #2
0
        public override void GetMathsInfo <T>()
        {
            this.clear();
            //传递参数
            var t = typeof(T);
            //var t = Type.GetType(className);

            List <Type> interfaces = t.GetInterfaces().Where(type => (!typeof(MsgController).IsAssignableFrom(type))).ToList();

            if (interfaces.Count > 1 || interfaces.Count == 0)
            {
                throw new Exception("MsgController的实现类必须继承Sharing中共享的接口," +
                                    "且不能继承MsgController外的其他接口,且不能多层继承");
            }

            for (int k = 0; k < interfaces.Count; k++)
            {
                this.ControllerObj     = Activator.CreateInstance(t);
                this.packageName       = t.Namespace;
                this.FullName          = t.FullName;
                this.className         = t.Name;
                this.interfaceFullName = interfaces[k].FullName;
                try
                {
                    #region 方法二
                    MethodInfo[] info = t.GetMethods();
                    for (int i = 0; i < info.Length; i++)
                    {
                        MsgFun mf = new MsgFun();

                        MethodInfo md = info[i];
                        if (md.Name.Equals("ToString") || md.Name.Equals("Equals") || md.Name.Equals("GetHashCode") || md.Name.Equals("GetType"))
                        {
                            continue;
                        }
                        //方法名
                        string mothodName = md.Name;
                        Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name}");
                        //参数集合
                        ParameterInfo[] paramInfos = md.GetParameters();

                        MsgFun mfs = new MsgFun();
                        mfs.Name = md.Name;
                        mfs.rep  = md.ReturnType;

                        //var objj = Activator.CreateInstance(mfs.req);
                        mfs.methodInfo = md;
                        this.put(md.Name, mfs);

                        for (int j = 0; j < paramInfos.Length; j++)
                        {
                            ParameterInfo parameterInfo = paramInfos[j];
                            mfs.req = parameterInfo.ParameterType;

                            Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name},{"入参" + j + ":" + parameterInfo.ParameterType}");
                        }
                        FleckLog.Level = LogLevel.Debug;
                        var allSockets = new List <IWebSocketConnection>();
                        var server     = new WebSocketServer($"{sockettype }://{ipaddres}:{ ipport}//{this.interfaceFullName + "." + md.Name}");

                        Action <IWebSocketConnection> config = new Action <IWebSocketConnection>(socket =>
                        {
                            socket.OnOpen = () =>
                            {
                                allSockets.Add(socket);
                            };
                            socket.OnClose = () =>
                            {
                                Console.WriteLine();
                                allSockets.Remove(socket);
                            };
                            socket.OnMessage = message =>
                            {
                                SuperMsg superMsg = this.serializer.DeSerializeString <SuperMsg>(message);
                                object[] objs     = new object[] { superMsg.msg };// new object[] { JsonSerializer.CreateDefault().Deserialize( ea.Body.ToArray().ToString(), parameterInfo.ParameterType) };//new object[] { ProtobufSerializer.DeSerializeBytes(parameterInfo.ParameterType, ea.Body.ToArray()) }
                                object rep        = mfs.methodInfo.Invoke(this.ControllerObj, objs);
                                if (md.ReturnType != typeof(void))
                                {
                                    //message 然后开启查询
                                    allSockets.ToList().ForEach(s => s.Send(this.serializer.SerializeString(new SuperMsg(rep))));
                                }
                            };
                        });
                        WeeklyAction weeklyAction = new WeeklyAction(SendMsg);
                        weeklyAction.Invoke(server, config);
                        Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name},{"返回值" + ":" + md.ReturnType}");
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }