/**
         * Decode the message by switching on the tag in the package.
         */
        private void HandleData(dynamic data)
        {
            string tag = data.tag;

            switch (tag)
            {
            case "encrypt/key/success":     // Initializes the new received encryption keys.
                byte[] key = decryptor.DecryptRsa(data.data.key.ToObject <byte[]>());
                byte[] iv  = decryptor.DecryptRsa(data.data.iv.ToObject <byte[]>());

                encryptor.AesKey = key;
                encryptor.AesIv  = iv;

                decryptor.AesKey = key;
                decryptor.AesIv  = iv;

                keyExchanged = true;
                break;

            case "client/register/success":
            case "client/login/success":     // Succesfully logged in or registered to the server.
                Console.WriteLine(data.data.message);
                loggedIn = true;
                break;

            case "client/register/error":
            case "client/login/error":     // Lets the client know that the input credentials were incorrect or was unable to register.
                Console.WriteLine($@"ERROR: {data.data.message}");
                break;

            case "session/resistance":     // Sets the new received resistance from the doctor.
                float resistance = float.Parse(data.data.resistance.ToObject <string>());
                co.WriteResistance(resistance);
                break;

            case "session/start":     // Starts the session, by starting the vr.
                Console.WriteLine(@"Start session");
                vpnConnector.Connect();
                vpnConnector.CommandCenter.Running = true;
                break;

            case "session/stop":     // Stops the session, by stopping the vr.
                Console.WriteLine(@"Stop session");
                vpnConnector.CommandCenter.ResetScene();
                vpnConnector.CommandCenter.Running = false;
                vpnConnector.Disconnect();
                break;

            case "chat/message":
            case "chat/broadcast":     // Displays the new received message to the client.
                string message = data.data.message.ToObject <string>();
                Console.WriteLine($@"Received Message: {message}");
                vpnConnector.CommandCenter.ChatMsg = message;
                break;
            }
        }
Esempio n. 2
0
        /**
         * Decode the message by switching on the tag in the package.
         */
        private void HandleData(dynamic data)
        {
            var jData = data as JObject;
            var tag   = jData?["tag"]?.ToObject <string>();

            switch (tag)
            {
            case "encrypt/key/success":     // Initializes the new received encryption keys.
                byte[] key = decryptor.DecryptRsa(data.data.key.ToObject <byte[]>());
                byte[] iv  = decryptor.DecryptRsa(data.data.iv.ToObject <byte[]>());

                encryptor.AesKey = key;
                encryptor.AesIv  = iv;

                decryptor.AesKey = key;
                decryptor.AesIv  = iv;

                keyExchanged = true;
                break;

            case "doctor/login/success":     // Logs the doctor in
                receivedLoginFeedback = true;
                loggedIn = true;
                break;

            case "doctor/login/error":     // Lets the doctor know that the input credentials were incorrect.
                receivedLoginFeedback = true;
                break;

            case "client/update/heartRate":     // Updates the heart rate of a connected client.
                layoutParent.NewHeartRate((string)data.data.clientId, (int)data.data.heartRate);
                break;

            case "client/update/speed":     // Updates the speed of a connected client.
                layoutParent.NewSpeed((string)data.data.clientId, (int)data.data.speed);
                break;

            case "doctor/newClient":     // Lets this application know a new client has connected to the server.
                layoutParent.NewClient((string)data.data.clientId, (string)data.data.name);
                break;

            case "client/disconnect":     // Lets this application know a client has disconnected from the server.
                layoutParent.RemoveClient((string)data.data.clientId);
                break;

            case "doctor/getSessions/fragment":     // Received a fragment of the records list from server.
                foreach (dynamic r in ((JArray)data.data.records).Children())
                {
                    records.Add(new SessionData(r));
                }
                GetNextFragment();
                break;

            case "doctor/getSessions/success":    // Received the final fragment of the records list from server.
                foreach (dynamic r in ((JArray)data.data.records).Children())
                {
                    records.Add(new SessionData(r));
                }
                layoutParent.RefreshHistoryPage(records);
                retreivedRecords = true;
                break;

            // Unused responses.
            case "chat/message/success":
            case "chat/broadcast/success":
            case "session/resistance/success":
            case "session/start/success":
            case "session/stop/success":
                Console.WriteLine(
                    $@"Success: {jData["data"]?.ToObject<JObject>()?["message"]?.ToObject<string>()}");
                break;

            case "doctor/clientHistory/error":
            case "chat/message/error":
            case "chat/broadcast/error":
            case "session/resistance/error":
            case "session/start/error":
            case "session/stop/error":
                Console.WriteLine($@"ERROR: {jData["data"]?.ToObject<JObject>()?["message"]?.ToObject<string>()}");
                break;
            }
        }