private void Connection_OnSocketConnectionReceivedSuccess(SocketConnection connection, byte[] data) { if (data.Length > 0) { SocketPackage package = SocketPackage.Unpackage(data); Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { switch (package.Action) { /// ============================= /// 身分驗證成功 /// ============================= case SocketPackage.EnumAction.AuthenticationResponse: _server.Connections.Where(x => x == connection).First().User = JsonConvert.DeserializeObject <User>(package.Data); ShowMessage(String.Format("user {0} was signed in", JsonConvert.DeserializeObject <User>(package.Data).Account)); ShowInformation(); break; /// ============================= /// 使用者登出 /// ============================= case SocketPackage.EnumAction.ClientSignout: _server.Connections.RemoveAll(x => x.User.Account == package.Data); ShowMessage(String.Format("user {0} was signed out", package.Data)); ShowInformation(); break; /// ============================= /// 傳遞訊息 /// ============================= case SocketPackage.EnumAction.ClientSendMessage: Message message = JsonConvert.DeserializeObject <Message>(package.Data); SocketConnection target = _server.Connections.Where(x => x.User.Account == message.To.Account).FirstOrDefault(); if (target != null) { target.Send(new SocketPackage() { Action = SocketPackage.EnumAction.ClientReciveMessage, Data = package.Data }); ShowMessage(String.Format("user {0} → user {1} (success): {2}", message.From.Name, message.To.Name, message.Content)); } else { ShowMessage(String.Format("user {0} → user {1} (failed): {2}", message.From.Name, message.To.Name, message.Content)); } break; } })); } }
// =============================================== // 自定義事件處理函式 // =============================================== #region [SocketClient][收到資料時] private void _client_OnSocketClientReceivedSuccess(SocketClient sender, byte[] data) { if (data.Length > 0) { SocketPackage package = SocketPackage.Unpackage(data); Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { switch (package.Action) { /// ============================= /// 伺服器端要求傳送身分資料 /// ============================= case SocketPackage.EnumAction.AuthenticationRequest: _client.Send(new SocketPackage() { Action = SocketPackage.EnumAction.AuthenticationResponse, Data = JsonConvert.SerializeObject(_user) }); pnlFrame.Navigate(new PageLobby(sender.User)); break; /// ============================= /// 伺服器端傳來訊息 /// ============================= case SocketPackage.EnumAction.ClientReciveMessage: Message message = JsonConvert.DeserializeObject <Message>(package.Data); ChattingRoomSingle window = Application.Current.Windows.OfType <ChattingRoomSingle>().Where(x => x.FriendAccount == message.From.Account).FirstOrDefault(); if (window != null) { window.pnlHistory.Children.Add(new TextBlock() { Text = message.Content, Padding = new Thickness(10), Margin = new Thickness(10), Background = Brushes.Blue, Foreground = Brushes.White, HorizontalAlignment = HorizontalAlignment.Left }); window.pnlScroller.ScrollToEnd(); window.Focus(); } else { ChattingRoomSingle room = new ChattingRoomSingle(message.From); room.pnlHistory.Children.Add(new TextBlock() { Text = message.Content, Padding = new Thickness(10), Margin = new Thickness(10), Background = Brushes.Blue, Foreground = Brushes.White, HorizontalAlignment = HorizontalAlignment.Left }); room.pnlScroller.ScrollToEnd(); room.Show(); } break; /// ============================= /// 伺服器端傳來廣播 /// ============================= case SocketPackage.EnumAction.ServerBroadcast: foreach (ChattingRoomSingle chattingRoom in Application.Current.Windows.OfType <ChattingRoomSingle>().ToList()) { chattingRoom.pnlHistory.Children.Add(new TextBlock() { Text = String.Format("System message: {0}", package.Data), Padding = new Thickness(10), Margin = new Thickness(10), Background = Brushes.Black, Foreground = Brushes.White, HorizontalAlignment = HorizontalAlignment.Center }); chattingRoom.pnlScroller.ScrollToEnd(); } break; /// ============================= /// 伺服器端關閉 /// ============================= case SocketPackage.EnumAction.ServerShutdown: _client.Disonnect(); pnlFrame.Navigate(new PageSignin()); break; } })); } }