Пример #1
0
        private void Ws_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            var    json     = JObject.Parse(e.Message);
            string typeName = (string)json["TypeName"];

            Type type = BaseMessageTypes.GetBaseMessageTypes().FirstOrDefault(x => x.Name.Equals(typeName));

            var request = JsonConvert.DeserializeObject(e.Message, type);

            OnMessageReceived?.Invoke(this, (IBaseMessage)request);
        }
Пример #2
0
        public void HandleMessage(object sender, string message)
        {
            Console.WriteLine(message);
            var    json     = JObject.Parse(message);
            string typeName = (string)json["TypeName"];

            Type type = BaseMessageTypes.GetBaseMessageTypes().FirstOrDefault(x => x.Name.Equals(typeName));

            var request = JsonConvert.DeserializeObject(message, type);

            // System.Console.WriteLine($"[{request.ID}] [{request.Command}] ", Color.Cornsilk);



            if (type == typeof(SubscribeRequest))
            {
                SubscribeRequest subscribeRequest = (SubscribeRequest)request;
                string           path             = subscribeRequest.Path;
                var id = subscribeRequest.ID;
                SubscribedCatalog.Add(id, path);

                watcher.Path         = path;
                watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                       | NotifyFilters.FileName | NotifyFilters.DirectoryName;


                watcher.Filter              = "*.txt";
                watcher.Changed            += new FileSystemEventHandler(OnChanged);
                watcher.Renamed            += new RenamedEventHandler(OnChanged);
                watcher.Created            += new FileSystemEventHandler(OnCreated);
                watcher.Deleted            += new FileSystemEventHandler(OnCreated);
                watcher.EnableRaisingEvents = true;
            }

            /*else if (request.Command == ValidCommand.Division)
             * {
             *  try
             *  {
             *      var tokens = request.Message.Split();
             *
             *      var a = tokens[1];
             *      var b = tokens[2];
             *      Int32.TryParse(a, out int resA);
             *      Int32.TryParse(b, out int resB);
             *      var div = resA / resB;
             *
             *      Socket.Send(div.ToString());
             *  }
             *  catch (Exception e)
             *  {
             *      Socket.Send(e.Message + "\n " + e.StackTrace);
             *  }
             * }*/
            /* else
             * {
             *   var r = new Request()
             *   {
             *       Command = ValidCommand.Unknown,
             *   };
             *   SendMessage(r);
             * }*/
        }