Example #1
0
    private void _message(object sender, MessageEventArgs e)
    {
        // Debug.Log(e.Data.ToString());

        // HANDLE MESSAGES

        if (!LoggedIn)
        {
            RES_LOG_CLASS FirstMsg = JsonUtility.FromJson <RES_LOG_CLASS>(e.Data.ToString());

            Debug.Log(FirstMsg.result[0].username);
            if (FirstMsg.result != null)
            {
                if (FirstMsg.result[0].currentOSUsername == FirstMsg.result[0].loggedInOSUsername)
                {
                    Debug.Log("You are logged in, " + FirstMsg.result[0].username + "!");
                    LoggedIn = true;
                    Emit("UserLoggedIn");
                }
                else
                {
                    Debug.LogError("Someone is apparently already logged in on another device.");
                }
            }
            else
            {
                Debug.LogError("Please login to your Emotiv App and try again.");
            }
        }
        else
        {
            RES_CLASS msg = JsonUtility.FromJson <RES_CLASS>(e.Data.ToString());

            // requestAccess
            if (msg.result.accessGranted)
            {
                if (msg.result.accessGranted)
                {
                    Debug.Log("Access Granted!");
                    Emit("AccessGranted");
                }
                else
                {
                    Debug.LogError("You do not granted access right to this application. Please use Emotiv App to proceed.");
                    Debug.LogError("If you have already approved this app via the Emtiv app, restart this application.");
                }
            }

            // authorize
            if (msg.result.cortexToken != null)
            {
                TOKEN = msg.result.cortexToken;
                Debug.Log("Authorized");
                Emit("Authorized");
            }

            // WEIRD ERROR WITH WEBSOCKET?
            //if (msg.result.warning.message != null)
            //{
            //    Debug.LogError("Warning Code " + msg.result.warning.code + ": " + msg.result.warning.message);
            //}

            // createSession
            if (msg.result.id != null)
            {
                SESSION = msg.result.id;
                Emit("SessionCreated");
                // activateSession();
            }

            // loadProfile
            if (msg.result.action != null && msg.result.name != null)
            {
                if (msg.result.action == "load")
                {
                    Debug.Log(msg.result.message + " : " + msg.result.name);
                    // Now Everything is set and done;
                    READY = true;
                    Emit("READY");
                }
                else
                {
                    Debug.LogWarning("Profile Saved!");
                }
            }

            // STREAM

            if (msg.result.failure != null)
            {
                Debug.Log("Stream Pending");
            }

            // Commands
            if (msg.com != null)
            {
                BRAIN.command = msg.com[0].ToString();
            }

            // FaceActions
            if (msg.fac != null)
            {
                BRAIN.eyeAction       = msg.fac[0].ToString();
                BRAIN.upperFaceAction = msg.fac[1].ToString();
                BRAIN.lowerFaceAction = msg.fac[3].ToString();
            }

            // Training
            if (msg.result.action != null && msg.result.status != null)
            {
                if (msg.result.status == "accept")
                {
                    Debug.LogWarning(msg.result.message);
                }
            }
            if (msg.sys != null)
            {
                switch (msg.sys[1])
                {
                case "MC_Started":
                    Emit("trainingStarted");
                    Debug.LogWarning($"Training Started");
                    break;

                case "MC_Succeeded":
                    Emit("trainingSucceeded");
                    Debug.LogWarning($"Training Succeeded");
                    break;

                case "MC_Completed":
                    Emit("trainingCompleted");
                    Debug.LogWarning($"Training Completed");
                    break;

                default:
                    Debug.LogWarning($"Emotiv System Message: { msg.sys[1] }");
                    break;
                }
            }

            if (msg.fac != null || msg.com != null)
            {
                Emit("STREAM");
            }
        }
    }
    private void _message(object sender, MessageEventArgs e)
    {
        // Debug.Log("WebSocket server said: " + e.Data);

        RES_CLASS msg = JsonUtility.FromJson <RES_CLASS>(e.Data.ToString());

        if (msg.result._auth != null)
        {
            this.TOKEN = msg.result._auth;
            Debug.Log("AUTH..." + TOKEN);
            this.Emit("authorized");
        }

        if (msg.result.appId != null)
        {
            Debug.Log("Create Session");
            this.SESSION = msg.result.id;
            this.Emit("createdSession");
        }

        if (msg.com != null)
        {
            this.BRAIN.command = msg.com[0].ToString();
            if (!this.STREAMREADY)
            {
                this.Emit("subscribed");
            }
        }

        if (msg.fac != null)
        {
            this.BRAIN.eyeAction       = msg.fac[0].ToString();
            this.BRAIN.upperFaceAction = msg.fac[1].ToString();
            this.BRAIN.lowerFaceAction = msg.fac[3].ToString();
        }


        if (msg.sys != null)
        {
            Debug.Log(msg);
            if (msg.sys[1] == "MC_Started")
            {
                this.Emit("trainingStarted");
            }
            else if (msg.sys[1] == "MC_Succeeded")
            {
                this.Emit("trainingSucceeded");
            }
            else if (msg.sys[1] == "MC_Completed")
            {
                this.Emit("trainingCompleted");
            }
            else
            {
                Debug.Log($"TrainingsMessage: { msg }");
            }
        }

        if (this.STREAMREADY || msg.fac != null || msg.com != null)
        {
            this.Emit("Stream");
        }
    }