public async Task ConnectAsync(string hostname, int port, Context context)
        {
            _conversationsRecords = await SqlHelper <ConversationsRecords> .CreateAsync();

            _context = context;
            try {
                var options = new IO.Options {
                    ForceNew     = true,
                    Reconnection = true,
                    Secure       = false,
                    //Transports = new string[] { EngineIO.Client.Transports.PollingXHR.TransportName},
                    Query = $"token={Utils.GetDefaults("Token")}&imei={Utils.GetDeviceIdentificator(Application.Context)}"
                };
                Socket = IO.Socket(hostname, options);

                Socket.On(Socket.EventConnect, OnConnect);
                Socket.On(Socket.EventDisconnect, OnDisconnect);
                Socket.On(Socket.EventConnectError, OnConnectError);
                Socket.On(Socket.EventConnectTimeout, OnConnectTimeout);
                //_socket.On(Manager.EventTransport, OnTransport);

                Socket.On("conversation", OnConversation);
                Socket.On("chat request", OnChatRequest);
                Socket.On("chat accepted", OnChatAccepted);
                Socket.On("chat rejected", OnChatRejected);
                Socket.On("Error", OnError);

                Socket.Connect();
                //Client = _socket;
            } catch (Exception e) {
                Log.Error("WSConnectionError: ", e.ToString());
            }
        }
        //connect client to tcp
        void ConnectToServer()
        {
            //socket = IO.Socket("http://192.168.8.100:5000/train");
            socket = IO.Socket("https://navigo.serveo.net/train");

            socket.On(Socket.EventConnect, (data) => {
            });

            socket.Connect();
        }
Example #3
0
 private void Init(string path, string query)
 {
     if (query != null)
     {
         var opts = new IO.Options()
         {
             Query = query
         };
         sock = IO.Socket(path, opts);
     }
     else
     {
         sock = IO.Socket(path);
     }
 }
Example #4
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            edtBetValue = FindViewById <EditText>(Resource.Id.edtGuessedNumber);
            edtMoney    = FindViewById <EditText>(Resource.Id.edtMoney);

            txtCount  = FindViewById <TextView>(Resource.Id.txtCount);
            txtScore  = FindViewById <TextView>(Resource.Id.txtScore);
            txtResult = FindViewById <TextView>(Resource.Id.txtResult);
            txtMoney  = FindViewById <TextView>(Resource.Id.txtMoney);
            txtStatus = FindViewById <TextView>(Resource.Id.txtStatus);

            btnSubmit        = FindViewById <Button>(Resource.Id.btnSubmitBet);
            btnSubmit.Click += delegate {
                try
                {
                    if (canPlay)
                    {
                        if (!isBet)
                        {
                            int betValueMoney = Convert.ToInt32(edtMoney.Text.ToString());
                            if (Common.score >= betValueMoney)
                            {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.Put("money", betValueMoney);
                                jsonObject.Put("betValue", Convert.ToInt32(edtBetValue.Text.ToString()));


                                socket.Emit("client_send_money", jsonObject);

                                Common.score -= betValueMoney;
                                txtScore.Text = Convert.ToString(Common.score);
                                isBet         = true;
                            }
                            else
                            {
                                Toast.MakeText(this, "You do not have enough money", ToastLength.Short).Show();
                            }
                        }
                        else
                        {
                            Toast.MakeText(this, "You already bet in this turn.", ToastLength.Short).Show();
                        }
                    }
                    else
                    {
                        Toast.MakeText(this, "Please wait until to next turn.", ToastLength.Short).Show();
                    }
                }
                catch (Exception ex)
                {
                    Toast.MakeText(this, ex.Message, ToastLength.Short).Show();
                }
            };

            btnDisconnect        = FindViewById <Button>(Resource.Id.btnDisconnect);
            btnDisconnect.Click += delegate {
                if (!isDisconnect)
                {
                    socket.Disconnect();
                    btnDisconnect.Text = "CONNECT";
                }
                else
                {
                    socket.Connect();
                    btnDisconnect.Text = "DISCONNECT";
                }

                isDisconnect = !isDisconnect;
            };


            try
            {
                socket = IO.Socket("http://10.0.2.2:3000");
                socket.On(Socket.EventConnect, new MyConnectEventListener(this));
                socket.Connect();
            }
            catch (System.Exception ex)
            {
                Toast.MakeText(this, ex.Message, ToastLength.Short).Show();
            }


            //Register all event from socket
            RegisterListeningEvent();
        }