signar与mysql服务器和xamarin客户端在多次成功调用后停止工作

wrrgggsh  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(280)

我正在visualstudiosxamarin中使用signar、mysql和iis创建一个android应用程序。我花了很多时间试图找到一个解决办法,但什么也找不到。基本上我有一个用户数据库和一个应用程序,可以让他们登录、注销和注册。该应用程序工作了好几次,登录、注销和注册用户,但最终挂起,没有抛出任何异常,除了与后端执行任何交互外,该应用程序仍然是交互式的。我已经检查了我的stacktrace没有运气,它似乎试图连接,然后什么也没有发生。最初,所有的用户交互都在一个对话框片段中,所以我在自己的活动中尝试了它们,但仍然收到相同的错误。我第一次使用microsoftsql服务器也是这么做的,希望这是一个愚蠢的ms安全问题,我没有运气就转用了mysql。我已经关闭了我的防火墙没有运气,并检查了我的所有iis设置。我以前用同样的基本设置构建过应用程序,以前从来没有遇到过这种情况,也没有机会修复它。
客户:主要活动

using Android.App;
using Android.Widget;
using Android.OS;
using Microsoft.AspNet.SignalR.Client;
using ChipTracker.Objects;
using System.Collections.Generic;
using Android.Content.PM;
using Android.Views;
using System.Threading.Tasks;
using System;
using Android.Content;
using MySql.Data;
using Plugin.CurrentActivity;
using MySql.Data.MySqlClient;

namespace ChipTracker
{
    [Activity(Label = "ChipTracker", MainLauncher = true, Theme =         "@android:style/Theme.NoTitleBar", ScreenOrientation =     ScreenOrientation.Landscape)]
public class MainActivity : Activity, IDialogInterfaceOnDismissListener
{
    private Button btnSignUp, btnSignIn;
    List<User> users = new List<User>();
    ListView userListView;
    public IHubProxy RemoteHubProxy;
    public HubConnectionTask hubConnection;
    protected override async void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);            
        hubConnection = new HubConnectionTask();
        RemoteHubProxy = await hubConnection.HubConnection();            
        SetContentView(Resource.Layout.Main);
        userListView = FindViewById<ListView>(Resource.Id.onlineUsersListView);
        btnSignUp = FindViewById<Button>(Resource.Id.btnSignUp);
        btnSignUp.Click += BtnSignUp_Click;
        btnSignIn = FindViewById<Button>(Resource.Id.btnSignIn);
        btnSignIn.Click += BtnSignIn_Click;
        int uiOptions = (int)Window.DecorView.SystemUiVisibility;
        uiOptions |= (int)SystemUiFlags.LowProfile;
        uiOptions |= (int)SystemUiFlags.Fullscreen;
        uiOptions |= (int)SystemUiFlags.HideNavigation;
        uiOptions |= (int)SystemUiFlags.ImmersiveSticky;
        Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;

        GetWhoseOnline();
    }

    private void BtnSignIn_Click(object sender, EventArgs e)
    {
        FragmentTransaction transaction = FragmentManager.BeginTransaction();
        dialog_SignIn signInDialog = new dialog_SignIn();
        signInDialog.Show(transaction, "signin fragment");

    }

    private void BtnSignUp_Click(object sender, System.EventArgs e)
    {

        Intent intent = new Intent(this, typeof(SignUpActivity));
        this.StartActivity(intent);
        this.Finish();
    }

    public async void GetWhoseOnline()
    {  
        RemoteHubProxy.On<List<User>>("whosOnline", (receivedList) =>
        {
            RunOnUiThread(() =>
            {
                users = receivedList;
                UserAdapter userAdapter = new UserAdapter(this, users);
                userListView.Adapter = userAdapter;
            });
        });
        await RemoteHubProxy.Invoke("WhosOnline");

    }

    public void OnDismiss(IDialogInterface dialog)
    {
        GetWhoseOnline();
    }

}
}

客户端:hubconnection

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Microsoft.AspNet.SignalR.Client;
using ChipTracker.Objects;

namespace ChipTracker
{
    public class HubConnectionTask
    {
        string address = "http://192.168.100.12/PokerBackend/";
        public async Task<IHubProxy> HubConnection()
        {
           Console.WriteLine("Connection Start");
            var hubConnection = new HubConnection(address);
           hubConnection.TraceLevel = TraceLevels.All;
           hubConnection.TraceWriter = Console.Out;
            var remoteHubProxy = hubConnection.CreateHubProxy("RemoteHub"); 
            try
            {
                await hubConnection.Start();

            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return remoteHubProxy;
        }
}

客户端:登录对话框

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.Views.Animations;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Microsoft.AspNet.SignalR.Client;
using ChipTracker.Objects;
using Plugin.CurrentActivity;

namespace ChipTracker
{
    class dialog_SignIn : DialogFragment
    {
        public IHubProxy RemoteHubProxy;
        private EditText txtUserName;
        private EditText txtPin;
        private TextView txtError;
        private Button btnSignIn;
        private RelativeLayout box;
        public Animation shake;
        public HubConnectionTask hubConnection;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            var view = inflater.Inflate(Resource.Layout.dialog_sign_in, container, false);
            hubConnection = new HubConnectionTask();
            txtError = view.FindViewById<TextView>(Resource.Id.textDialogErrorSignIn);
            txtUserName = view.FindViewById<EditText>(Resource.Id.txtUserNameSignIn);
            txtPin = view.FindViewById<EditText>(Resource.Id.txtPinSignIn);            
            btnSignIn = view.FindViewById<Button>(Resource.Id.btnDialogSignIn);
            btnSignIn.Click += BtnSignIn_Click;
            box = view.FindViewById<RelativeLayout>(Resource.Id.dialogBoxSignIn);
            return view;
        }

        private async void BtnSignIn_Click(object sender, EventArgs e)
        {
            User user = new User();

                if (txtPin.Text != "")
                {
                    RemoteHubProxy = await hubConnection.HubConnection();
                    user.pin = Convert.ToInt32(txtPin.Text);
                    if (txtUserName.Text == "")
                    {
                        txtError.Text = "Username is blank.";
                        shake = AnimationUtils.LoadAnimation(Application.Context, Resource.Animation.shake);
                        box.StartAnimation(shake);
                    }
                    else
                    {
                        user.username = txtUserName.Text;
                        user.online = "y";
                        user.start_time = DateTime.Now.ToString("HH:mm MM/dd");
                        LoginUser(user);

                }
                }
                else
                {

                    txtError.Text = "Pin is blank.";
                    shake = AnimationUtils.LoadAnimation(Application.Context, Resource.Animation.shake);
                    box.StartAnimation(shake);
                }

        }

        private async void LoginUser(User user)
        {
            RemoteHubProxy = await hubConnection.HubConnection();
            try
            {
                RemoteHubProxy.On<String>("loginUserSuccess", (success) =>
                {

                        Activity.RunOnUiThread(() =>
                        {
                            if (success == "y")
                            {

                                this.Dismiss();
                            }
                            if (success == "n" || success == null)
                            {
                                txtError.Text = "Unsuccessful Login. Please try again.";
                                shake = AnimationUtils.LoadAnimation(Application.Context, Resource.Animation.shake);
                                box.StartAnimation(shake);
                            }

                        });
                 });

                    await RemoteHubProxy.Invoke("LoginAsync", new object[] { user });

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        public override void OnDismiss(IDialogInterface dialog)
        {
            hubConnection.EndHubConnection();
            base.OnDismiss(dialog);
            Activity activity = this.Activity;
            ((IDialogInterfaceOnDismissListener)activity).OnDismiss(dialog);
        }

        public override void OnActivityCreated(Bundle savedInstanceState)
        {

            Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
            base.OnActivityCreated(savedInstanceState);
            Dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation;
        }

    }
}

信号服务器:remotehub

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Globalization;
using System.Text.RegularExpressions;
using Renci.SshNet;
using PokerBackend.Objects;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data;

namespace PokerBackend
{
    public class RemoteHub : Hub
    {

        public void WhosOnline()
        {
            MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["localConnection"].ConnectionString);
            List<User> userList = new List<User> { };
            MySqlCommand MySqlCommandSelect = new MySqlCommand("SELECT * FROM user_table WHERE isLoggedIn='y' AND isAdmin='n'", conn);
            conn.Open();
            MySqlDataReader reader = MySqlCommandSelect.ExecuteReader();
            while (reader.Read())
            {
                User user = new User();
                user.username = Convert.ToString(reader.GetValue(0));
                user.pin = Convert.ToInt16(reader.GetValue(1));
                userList.Add(user);
            }
            reader.Close();
            conn.Close();
            Clients.All.whosOnline(userList);

        }
        public void CreateUser (User user)
        {
            MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["localConnection"].ConnectionString);
            MySqlCommand MySqlCommandCreate = new MySqlCommand("INSERT INTO user_table (user_name, pin, isAdmin, isLoggedIn, start_time) VALUES (@username, @pin, @isAdmin, @isLoggedIn, @start_time)", conn);
            MySqlCommandCreate.Parameters.AddWithValue("@username", user.username);
            MySqlCommandCreate.Parameters.AddWithValue("@pin", Convert.ToInt32(user.pin));
            MySqlCommandCreate.Parameters.AddWithValue("@isAdmin", user.admin);
            MySqlCommandCreate.Parameters.AddWithValue("@isLoggedIn", user.online);
            MySqlCommandCreate.Parameters.AddWithValue("@start_time", user.start_time);
            // MySqlCommandCreate.Parameters.AddWithValue("@end_time", user.end_time);
            //MySqlCommandCreate.Parameters.AddWithValue("@total_time", user.total_time);
            //MySqlCommandCreate.Parameters.AddWithValue("@rewards", user.rewards);
            conn.Open();
            try
            {                
                MySqlCommandCreate.ExecuteNonQuery();
                Clients.All.createUserSuccess("y");

            }
            catch (MySqlException ex)
            {
                Clients.All.createUserSuccess("n");

            }
            finally
            {
                conn.Close();
            }
        }
        public void Logout (User user)
        {
            MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["localConnection"].ConnectionString);
            User userDB = new User();
            MySqlCommand MySqlCommandLogout = new MySqlCommand("UPDATE user_table SET isLoggedIn = 'n', end_time = @end_time WHERE user_name = @username", conn);
            MySqlCommandLogout.Parameters.AddWithValue("@username", user.username);
            //MySqlCommandCreate.Parameters.AddWithValue("@pin", user.pin);
            //MySqlCommandCreate.Parameters.AddWithValue("@isAdmin", user.admin);
            MySqlCommandLogout.Parameters.AddWithValue("@isLoggedIn", "n");
            MySqlCommandLogout.Parameters.AddWithValue("@end_time", user.end_time);
            conn.Open();
            try
            {

                MySqlCommandLogout.ExecuteNonQuery();
                Clients.All.logoutUserSuccess("y");

            }
            catch (MySqlException ex)
            {
                Clients.All.logoutUserSuccess("n");
                Console.WriteLine(ex);

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Clients.All.logoutUserSuccess("n");

            }

            finally
            {
                conn.Close();
            }
        }

        public void LoginAsync(User user)
        {
            User userDB = new User();
            MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["localConnection"].ConnectionString);
            MySqlCommand MySqlCommandSelect = new MySqlCommand("", conn);
            conn.Open();

            try
            {
                MySqlCommandSelect = new MySqlCommand("SELECT * FROM user_table WHERE user_name=@username AND pin=@pin", conn);
                MySqlCommandSelect.Parameters.AddWithValue("@username", user.username);
                MySqlCommandSelect.Parameters.AddWithValue("@pin", user.pin);

                MySqlDataReader reader = MySqlCommandSelect.ExecuteReader();
                while (reader.Read())
                {
                    userDB.username = Convert.ToString(reader.GetValue(0));       //user_name
                    userDB.pin = Convert.ToInt32(reader.GetValue(1));       //pin
                    //userDB.admin = Convert.ToString(reader.GetValue(2)); // isAdmin
                    userDB.online = Convert.ToString(reader.GetValue(3));  //isLoggedIn
                    //userDB.start_time = Convert.ToString(reader.GetValue(4));  //start_time
                    //userDB.end_time = Convert.ToString(reader.GetValue(5));   //end_time
                    //userDB.total_time = Convert.ToString(reader.GetValue(6));   //total_time
                    //userDB.rewards = Convert.ToInt32(reader.GetValue(7));       //rewards             
                }

                reader.Close();
                if (userDB.online == "y")
                {
                    Clients.All.loginUserSuccess("n");

                }

                if (user.pin == userDB.pin && userDB.online == "n")
                {
                    MySqlCommand MySqlCommandLogin = new MySqlCommand("UPDATE user_table SET isLoggedIn='y', start_time=@start_time WHERE user_name=@username", conn);
                    MySqlCommandLogin.Parameters.AddWithValue("@username", user.username);
                    //MySqlCommandCreate.Parameters.AddWithValue("@pin", user.pin);
                    //MySqlCommandCreate.Parameters.AddWithValue("@isAdmin", user.admin);
                    //MySqlCommandCreate.Parameters.AddWithValue("@isLoggedIn", user.online);
                    MySqlCommandLogin.Parameters.AddWithValue("@start_time", DateTime.Now.ToString("HH:mm MM/dd"));
                    //MySqlCommandCreate.Parameters.AddWithValue("@end_time", user.end_time);
                    //MySqlCommandCreate.Parameters.AddWithValue("@total_time", user.total_time);
                    //MySqlCommandCreate.Parameters.AddWithValue("@rewards", user.rewards);
                    MySqlCommandLogin.ExecuteNonQuery();
                    Clients.All.loginUserSuccess("y");

                }
                if (user.pin != userDB.pin)
                {
                    Clients.All.loginUserSuccess("n");

                }

            }

            catch (MySqlException ex)
            {
                Clients.All.loginUserSuccess("n");
                Console.WriteLine(ex);

            }
            catch (Exception e)
            {
                Clients.All.loginUserSuccess("n");
                Console.WriteLine(e);

            }
            finally
            {
                conn.Close();
            }

        }

    }
}

我知道所有的东西都是以纯文本的形式传递的,一旦我开始工作,我就会添加加密。谢谢大家!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题