Exemplo n.º 1
0
        public async Task Echo()
        {
            if (_state != "Matlab")
            {
                _timer = new Timer(DoWork, null, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(1000));
            }
            while (WebSocket.State == WebSocketState.Open)
            {
                var text = WebSocketHelper.GetMessage(WebSocket).Result;
                lock (_locker)
                {
                    IsConnect = true;
                }
                var code    = text.Split(":")[0];
                var message = text.Split(":")[1].Split(" ");
                stopWatch = Stopwatch.StartNew();
                Logger.Write(text);
                switch (code)
                {
                case "Measurements":
                    // Task.Factory.StartNew(() => ReceiveMeasurments(message));
                    ReceiveMeasurments(message);
                    break;

                case "GNSSClock":
                    // Task.Factory.StartNew(() => ReceiveGNSSClock(message));
                    ReceiveGNSSClock(message);
                    break;

                case "GetEphemerides":
                    //  Task.Factory.StartNew(() => returnEphemerides());
                    returnEphemerides();
                    break;

                case "Location":
                    ReceiveLocation(message);
                    break;

                case "Check":
                    break;

                case "Close":
                    break;
                }
            }
            await WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closed from server", CancellationToken.None);
        }
Exemplo n.º 2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, Microsoft.Extensions.Hosting.IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseWebSockets();


            app.Use(async(context, next) =>
            {
                if (context.Request.Path == "/ws")
                {
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
                        Console.WriteLine($"New Connection {context.Connection.RemoteIpAddress}");
                        var message           = WebSocketHelper.GetMessage(webSocket).Result;
                        var connectionmessage = message.Split(":");

                        switch (connectionmessage[0])
                        {
                        case "Matlab":
                            {
                                var user    = new Models.User(webSocket, Guid.NewGuid());
                                user._state = "Matlab";
                                Program.MatlabUser.Add(user);
                                Console.WriteLine($"New Connected MatlabUser {context.Connection.RemoteIpAddress} " + user._guid);
                                await user.Echo();
                                break;
                            }

                        case "Phone":
                            {
                                switch (connectionmessage[2])
                                {
                                case "Relative":
                                    {
                                        var user = new Models.User(webSocket, connectionmessage[0], Guid.NewGuid(), connectionmessage[1], connectionmessage[2], connectionmessage[3]);
                                        Console.WriteLine($"New Connected Phone if relative mode {context.Connection.RemoteIpAddress} " + connectionmessage[1] + " " + connectionmessage[3]);
                                        Program.Users.Add(user);
                                        await user.Echo();
                                        break;
                                    }

                                case "StandAlone":
                                    {
                                        var user = new Models.User(webSocket, connectionmessage[0], Guid.NewGuid(), connectionmessage[1], connectionmessage[2], connectionmessage[3]);
                                        Program.Users.Add(user);
                                        Console.WriteLine($"New Connected Phone if StandAlone mode {context.Connection.RemoteIpAddress} " + connectionmessage[1] + " " + connectionmessage[3]);

                                        await user.Echo();
                                        break;
                                    }
                                }
                                break;
                            }
                        }

                        /*var user = new Models.User(webSocket,  Guid.NewGuid());
                         * if (messageMass[0] == "State")
                         * {
                         *    if (messageMass[1] == "Matlab")
                         *    {
                         *        user._state = "Matlab";
                         *        Program.MatlabUser.Add(user);
                         *        Console.WriteLine($"New Connected MatlabUser {context.Connection.RemoteIpAddress} " + user._guid);
                         *    }
                         *    else if(messageMass[1] == "MainMatlabUser")
                         *    {
                         *        user._state = "Matlab";
                         *        Program.MainMatlabUser = user;
                         *        Console.WriteLine($"New Connected MainMatlabUser {context.Connection.RemoteIpAddress} " + user._guid);
                         *    }
                         *    else if(messageMass[1] == "Phone")
                         *    {
                         *        user._model = messageMass[2];
                         *        Program.Users.Add(user);
                         *        Console.WriteLine($"New Connected Phone {context.Connection.RemoteIpAddress} " + user._guid +"  " + messageMass[2]);
                         *       //Console.WriteLine($"New Connected Phone {context.Connection.RemoteIpAddress} " + user._guid);
                         *    }
                         *    await user.Echo();
                         * }
                         * else
                         * {
                         *    await user.Echo();
                         * }*/
                    }

                    else
                    {
                        context.Response.StatusCode = 400;
                    }
                }
                else
                {
                    await next();
                }
            });
            app.UseMvc();
        }
Exemplo n.º 3
0
        //чекаем прием нового сообщения
        public async Task Echo()
        {
            if (_state != "Matlab")
            {
                //_timer = new Timer(DoWork, null, TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(1000));
                //Sender._timer = new Timer(Sender.SendFullMessage, null, TimeSpan.FromMilliseconds(150), TimeSpan.FromMilliseconds(1000));
            }
            while (WebSocket.State == WebSocketState.Open)
            {
                time     = DateTime.Now.ToUniversalTime();
                interval = time - Gps0000z;

                //Console.WriteLine(interval.TotalMilliseconds);
                var text    = WebSocketHelper.GetMessage(WebSocket).Result;
                var mesType = WebSocketHelper.GetMessageType(WebSocket).Result;
                lock (_locker)
                {
                    IsConnect = true;
                }
                var code    = text.Split(":")[0];
                var message = text.Split(":")[1].Split(" ");

                switch (code)
                {
                case "Measurements":
                    // Task.Factory.StartNew(() => ReceiveMeasurments(message));
                    ReceiveMeasurments(message);
                    break;

                case "GNSSClock":
                    // Task.Factory.StartNew(() => ReceiveGNSSClock(message));
                    ReceiveGNSSClock(message);
                    break;

                case "GetEphemeris":
                    //  Task.Factory.StartNew(() => returnEphemerides());
                    returnEphemerides(message);
                    break;

                case "Location":
                    ReceiveLocation(message);
                    break;

                case "Check":
                    break;

                case "Close":
                    await WebSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Closed from client", CancellationToken.None);

                    //if(this.WebSocket.State == WebSocketState.)
                    Console.WriteLine("Client Closed: " + this._model);
                    break;

                case "Ping":
                    Pong(message);
                    break;

                case "Diff":
                    //обкатка относительной навигации между двумя телефами
                {
                    Console.WriteLine(string.Join(" ", message));
                    foreach (User user in Program.Users)
                    {
                        if (user._diff_mode == "Base")
                        {
                            SendMessage(user.WebSocket, string.Join(" ", message));
                        }
                    }
                }
                break;

                case "INS":
                    ReceiveINS(message);
                    break;

                case "GnssMeasurement":
                {
                    Console.WriteLine(_model + " " + string.Join(" ", message));
                    Logger.Write(_model + " " + string.Join(" ", message), _model);
                    break;
                }
                }
            }
            await WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closed from server", CancellationToken.None);
        }