Exemplo n.º 1
0
        /// <summary>
        /// Load the file content.
        /// </summary>
        /// <param name="method">method name</param>
        /// <param name="funcGetData">for data response.</param>
        /// <returns>Return the page or data in string.</returns>
        public string GetData(string method, NameValueCollection queryString)
        {
            if (!this._methods.ContainsKey(method))
            {
                SimpleLogging.Log($"unknow method: {method}");
                return("Missing method!");
            }

            var response = this._methods[method].Invoke(queryString);

            return(JsonConvert.SerializeObject(response));
        }
        internal static Response GetOnlineUser(NameValueCollection queryString)
        {
            if (long.TryParse(queryString.Get("id"), out long userId))
            {
                _dataManager.UpdateUserAlive(userId);

                return(new Response
                {
                    Success = true,
                    Content = _dataManager.GetUsers(userId)
                });
            }

            SimpleLogging.Log($"GetOnlineUser method has return no data.");
            return(Response.CreateFail("No data"));
        }
        internal static Response GetMessages(NameValueCollection queryString)
        {
            if (long.TryParse(queryString.Get("id"), out long userId) &&
                long.TryParse(queryString.Get("touserid"), out long toUserId))
            {
                return(new Response
                {
                    Success = true,
                    Content = _dataManager.GetMessages(userId, toUserId)
                });
            }

            var sb = new StringBuilder();

            foreach (var item in queryString.AllKeys)
            {
                sb.Append($"{item}, ");
            }
            SimpleLogging.Log($"id not valid. Keys: {sb}");
            return(Response.CreateFail("ERROR: User id not valid"));
        }
        static void Main(string[] args)
        {
            SimpleLogging.Log("Server start");
            var messenger = new MessengerManager();
            var ws        = new Webserver("http://localhost:5000/",
                                          request =>
            {
                Console.WriteLine("Received request:");
                Console.WriteLine($"RawUrl     => {request.RawUrl}");
                Console.WriteLine($"HttpMethod => {request.HttpMethod}");

                return(messenger.GetData(request.Url.AbsolutePath, request.QueryString));
            });

            ws.Run();
            Console.WriteLine("Press a key to quit.");
            Console.ReadKey();
            ws.Stop();

            SimpleLogging.Log("Server stopped");
        }