/// <summary> /// 连接服务器异步回调 /// </summary> /// <param name="message">发送的信息</param> public void ConnectCallBackF(IAsyncResult ar) { ConnectParam connectParam = (ConnectParam)ar.AsyncState; try { connectParam.tcpClient.EndConnect(ar); connectParam.IsConnect = true; connectParam.GetTcpClient(); //连接成功,然后开启线程等待接收消息 connectParam.ListenerMsgThread.Start(connectParam); //先建立SSL通道(利用公钥将对称key加密发给服务器) AsyncSendMessage(connectParam, RSAProcessor.RSAEncrypt(configModel.PublicKey, connectParam.RandomKey) + MessageTypes.KEY); //发送第一条用对称key加密的信息 AsyncSendMessage(connectParam, RijndaelProcessor.EncryptString(connectParam.Msg, connectParam.RandomKey)); } catch { App.Current.Dispatcher.Invoke(new Action(() => { StoryboardManager.StopStoryboard("Story_Login"); XWT.MessageBox.Show("未能连接到服务器,请检查网络状态!", "提示", MessageBoxButton.OK, MessageBoxImage.Error); }), System.Windows.Threading.DispatcherPriority.Normal); if (connectParam.tcpClient != null) { connectParam.tcpClient = null; } connectParam.IsConnect = false; } }
public void OnReceiveMsg(string message) { if (connectParam.IsConnect == true) { //直接发送加密信息 AsyncSendMessage(connectParam, RijndaelProcessor.EncryptString(message, connectParam.RandomKey)); } else { //先建立连接及SSL通道,再发送 ConnectToServer(message, configModel.LoginIP, int.Parse(configModel.LoginPort)); } //MsgEvent msgEvent = eventAggregator.GetEvent<MsgEvent>(); //if (subscriptionToken != null) // msgEvent.Unsubscribe(subscriptionToken); }
/// <summary> /// 侦听信息命令线程方法 /// </summary> private void ListenerMsgThreadMethod(object connectState) { ConnectParam connectParam = connectState as ConnectParam; string receiveString = null; while (connectParam.IsExit == false) { try { receiveString = connectParam.sr.ReadLine(); } catch (Exception e) { if (connectParam.IsExit == false) { App.Current.Dispatcher.Invoke(new Action(() => { //关闭连接 CloseConnect(connectParam); XWT.MessageBox.Show("与服务器断开连接!\n错误:" + e.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Warning); }), System.Windows.Threading.DispatcherPriority.Normal); } break; } if (receiveString != null) { receiveString = RijndaelProcessor.DencryptString(receiveString, connectParam.RandomKey); // ilogger.Logger(string.Format("接收到的数据:{0}", receiveString)); //解密、分割 string[] Msg = receiveString.Split(MessageTypes.PSP.ToCharArray()); //存储 receiveMsgOrder.ModuleType = Msg[0]; receiveMsgOrder.Sign = int.Parse(Msg[1]); receiveMsgOrder.MsgContent = Msg[2]; if (receiveMsgOrder.ModuleType == MessageTypes.UPD) { DealUpdate(receiveMsgOrder.MsgContent); } else { //广播 receive_Aggregator.GetEvent <ReceiveMsgEvent>().Publish(new ReceiveMsgOrder { ModuleType = Msg[0], Sign = int.Parse(Msg[1]), MsgContent = Msg[2] }); //连接成功,但操作异常 if (receiveMsgOrder.Sign == 0 && receiveMsgOrder.ModuleType == MessageTypes.CON) { break; } //登陆成功,返回“账号+昵称+头像”,并切换到大厅服务器 if (receiveMsgOrder.Sign == 1 && receiveMsgOrder.ModuleType == MessageTypes.LOG) { CloseConnect(connectParam); //建立新的连接及SSL通道,然后发送信息(CON++ID+账号) ConnectToServer(MessageTypes.CON + receiveMsgOrder.MsgContent.Split(MessageTypes.NSP.ToCharArray())[0] + MessageTypes.NSP + receiveMsgOrder.MsgContent.Split(MessageTypes.NSP.ToCharArray())[1], configModel.HallIP, int.Parse(configModel.HallPort)); break; } } } } }