private void MainForm_Load(object sender, EventArgs e) { // 注册连接打开的响应函数 WebSocketManager.AddConnectionOpenHandler(OnConnectionOpen); // 注册连接中断的响应函数 WebSocketManager.AddConnectionCloseHandler(OnConnectionClose); // 注册连接错误的响应函数 WebSocketManager.AddConnectionErrorHandler(OnConnectionError); // 注册一些服务器会主动push的消息 WebSocketManager.AddRpcHandler(GmRpcNameEnum.GmRpcForceOfflinePush, OnForceOfflinePushCallback); WebSocketManager.AddRpcHandler(GmRpcNameEnum.GmRpcTextMsgPush, OnTextMsgPushCallback); // 与GM服务器建立连接 WebSocketManager.InitWebSocket(); if (WebSocketManager.IsConnected() == false) { string tips = string.Format("无法连接GM服务器,请确保服务器地址为{0},且处于开启状态", AppValues.GM_SERVER_URL); MessageBox.Show(this, tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } // 强制弹出登录界面 loginForm = new LoginForm(); loginForm.ShowDialog(); }
private void btnLogin_Click(object sender, EventArgs e) { if (WebSocketManager.IsConnected() == false) { WebSocketManager.InitWebSocket(); if (WebSocketManager.IsConnected() == false) { string tips = string.Format("无法连接GM服务器,请确保服务器地址为{0},且处于开启状态", AppValues.GM_SERVER_URL); MessageBox.Show(this, tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } string inputUsername = txtUsername.Text.Trim(); string inputPassword = txtPassword.Text.Trim(); if (string.IsNullOrEmpty(inputUsername)) { MessageBox.Show("用户名不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(inputPassword)) { MessageBox.Show("密码不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } GmLoginRequest.Builder builder = GmLoginRequest.CreateBuilder(); builder.SetUsername(inputUsername); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string passwordMD5 = BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(inputPassword))).Replace("-", "").ToUpper(); builder.SetPasswordMD5(passwordMD5); WebSocketManager.SendMessage(GmRpcNameEnum.GmRpcLogin, builder.Build().ToByteArray(), OnLoginCallback); }