Example #1
0
        static void Main()
        {
            Console.Title = "OOP SERVER";

            try
            {
                Config <ServerSettings> settings = new Config <ServerSettings>("config.json");
                settings.Load();

                Console.Write("Load settings (yes / no)?: ");
                bool load = Console.ReadLine() == "yes";
                if (settings.Data == null || !load)
                {
                    settings.Data = new ServerSettings();

                    Console.Write("Server port: ");
                    int.TryParse(Console.ReadLine(), out int port);
                    if (port <= 0)
                    {
                        throw new Exception("Invalid port!");
                    }

                    Console.Write("Run over lan? (yes / no): ");
                    settings.Data.share_over_lan = Console.ReadLine() == "yes";
                    settings.Data.port           = port;
                    settings.Save();
                }

                string          url = $"ws://{(settings.Data.share_over_lan ? "0.0.0.0" : "127.0.0.1")}:{settings.Data.port}";
                WebSocketServer wss = new WebSocketServer(url);
                wss.AddWebSocketService <EngineServer>("/engine");
                url += "/engine";
                wss.Start();
                Console.WriteLine($"Server started on [{url}] Press any key to close server...");
                Console.ReadKey();
                wss.Stop();
            }
            catch (Exception ex)
            {
                ConsoleTools.DisplayError(ex.Message);
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }
        }
        protected override void OnMessage(MessageEventArgs e)
        {
            try
            {
                BaseRequest <object>  request  = JsonConvert.DeserializeObject <BaseRequest <object> >(e.Data);
                BaseResponse <object> response = new BaseResponse <object>
                {
                    method  = request.method,
                    error   = 0,
                    message = ""
                };
                if (request.method == RequestMethods.RequestSendSettings)
                {
                    if (_engine != null)
                    {
                        response.error   = 1;
                        response.message = "Engine already configured!";
                        goto send_rsp;
                    }

                    SetupSettings setup = ((request.data) as JObject)?.ToObject <SetupSettings>();
                    _engine = new Engine(setup?.engine, setup?.entity);
                    _engine.OnEntityChanged += EngineOnEntityChanged;
                    _engine.OnStatusChanged += EngineOnStatusChanged;
                    _engine.OnHourTick      += EngineOnHourTick;
                    _engine.OnNewDay        += EngineOnNewDay;

                    response.data = _engine.GetEntities();
                    goto send_rsp;
                }

                if (_engine == null)
                {
                    response.error   = 1;
                    response.message = "Engine not configured!";
                    goto send_rsp;
                }

                if (request.method == RequestMethods.RequestStartEmulation)
                {
                    if (_engine.EngineStatus)
                    {
                        response.error   = 1;
                        response.message = "Engine already started!";
                        goto send_rsp;
                    }
                    _engine.Start();
                }

                if (request.method == RequestMethods.RequestStopEmulation)
                {
                    if (!_engine.EngineStatus)
                    {
                        response.error   = 1;
                        response.message = "Engine not started!";
                        goto send_rsp;
                    }
                    _engine.Stop();
                }

send_rsp:

                Send(JsonConvert.SerializeObject(response));
            }
            catch (Exception ex)
            {
                ConsoleTools.DisplayError(ex.Message);
            }
        }