protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            //get controls from layout
            btnConnect = FindViewById <Button>(Resource.Id.btnConnect);
            btnSpeak   = FindViewById <Button>(Resource.Id.btnSpeak);
            rbMic      = FindViewById <RadioButton>(Resource.Id.rbMic);

            webSocket = new WebSocket("ws://10.92.84.183:80", "");

            //client socket connected
            webSocket.Opened += (s, e) =>
            {
                RunOnUiThread(() =>
                {
                    Toast.MakeText(this, "Connected.", ToastLength.Short).Show();
                    btnConnect.Enabled = false;
                    btnConnect.Text    = "Connected.";

                    btnSpeak.Enabled = true;
                });
            };

            //client socket disconnected
            webSocket.Closed += (s, e) =>
            {
                RunOnUiThread(() =>
                {
                    Toast.MakeText(this, "Disconnected.", ToastLength.Short).Show();
                    btnConnect.Enabled = true;
                    btnConnect.Text    = "Connect";

                    micState = MicState.Off;

                    btnSpeak.Enabled = false;
                });
            };

            //client socket received a message
            webSocket.MessageReceived += (s, e) =>
            {
                RootObject root = JsonConvert.DeserializeObject <RootObject>(e.Message); //deserialize json message into obj

                switch (root.Data.Type)
                {
                case "ping":
                    RunOnUiThread(() =>
                    {
                        Toast.MakeText(this, root.Data.Message, ToastLength.Short).Show();
                    });
                    break;

                case "mic":
                    RunOnUiThread(() =>
                    {
                        if (root.Data.On == true)
                        {
                            Toast.MakeText(this, root.Data.Message, ToastLength.Short).Show();
                            micState = MicState.On;
                        }
                        else
                        {
                            Toast.MakeText(this, root.Data.Message, ToastLength.Short).Show();
                            micState = MicState.Off;
                        }
                    });
                    break;
                }
            };

            //connect to websocket sv
            btnConnect.Click += (s, e) =>
            {
                if (webSocket.State == WebSocketState.Closed || webSocket.State == WebSocketState.None)
                {
                    webSocket.Open();
                }
            };

            //send request to sv to speak/turn mic on
            btnSpeak.Click += (s, e) =>
            {
                micState = MicState.Requesitng;

                RootObject root = new RootObject();
                root.Data.Type = "request";
                string json = JsonConvert.SerializeObject(root);
                webSocket.Send(json);
            };
        }
示例#2
0
 private void HandleLoaded(RootObject rootObj)
 {
     ProgramListComponent.ToggleFullLoading(false);
     ProgramListComponent.DisplayEntries(rootObj.Data);
 }