| | |
| | | using DevComponents.DotNetBar; |
| | | using System.Windows; |
| | | using System.Drawing; |
| | | using System.Net.WebSockets; |
| | | using System; |
| | | using System.Threading.Tasks; |
| | | using System.Text; |
| | | using System.Diagnostics; |
| | | using System.Timers; |
| | | using System.Collections.ObjectModel; |
| | | using System.ComponentModel; |
| | | using System.Runtime.CompilerServices; |
| | | using PdmAlert.Entity; |
| | | |
| | | namespace PdmAlert |
| | | { |
| | | /// <summary> |
| | | /// MainWindow.xaml 的交互逻辑 |
| | | /// </summary> |
| | | public partial class MainWindow : Window |
| | | public partial class MainWindow : Window, INotifyPropertyChanged |
| | | { |
| | | #region INotifyPropertyChanged |
| | | public virtual event PropertyChangedEventHandler PropertyChanged; |
| | | |
| | | protected virtual void RaisePropertyChanged(string name) |
| | | { |
| | | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); |
| | | } |
| | | |
| | | protected void RaiseAndSetIfChanged<T>(ref T old, T @new, [CallerMemberName] string propertyName = null) |
| | | { |
| | | old = @new; |
| | | if (propertyName != null) |
| | | { |
| | | RaisePropertyChanged(propertyName); |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | private ClientWebSocket socket; |
| | | private Task readTask; |
| | | private Task writeTask; |
| | | private System.Timers.Timer heartbeatTimer; |
| | | |
| | | private string _messageTitle = "共有 0 条未读消息"; |
| | | |
| | | public string messageTitle |
| | | { |
| | | get => _messageTitle; |
| | | set => RaiseAndSetIfChanged(ref _messageTitle, value); |
| | | } |
| | | |
| | | private ObservableCollection<MsgData> _messages; |
| | | |
| | | public ObservableCollection<MsgData> messages |
| | | { |
| | | get => _messages; |
| | | set => RaiseAndSetIfChanged(ref _messages, value); |
| | | } |
| | | |
| | | public MainWindow() |
| | | { |
| | | InitializeComponent(); |
| | | } |
| | | |
| | | private long _RunningAlertId = 0; |
| | | private void Button_Click(object sender, RoutedEventArgs e) |
| | | private void Refresh_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | eDesktopAlertColor color = eDesktopAlertColor.Default; |
| | | eAlertPosition position = eAlertPosition.BottomRight; |
| | | DesktopAlert.Show("123", "\uf005", eSymbolSet.Awesome, Color.Empty, color, position, 10, ++_RunningAlertId, AlertClicked); |
| | | |
| | | |
| | | } |
| | | |
| | | private void SwitchUser_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | |
| | | |
| | | } |
| | | |
| | | private void AlertClicked(long alertId) |
| | | { |
| | | |
| | |
| | | |
| | | this.Left = screeWidth - this.Width; |
| | | this.Top = sHeight - this.Height; |
| | | ConnectWebSocket(); |
| | | } |
| | | |
| | | private async Task ConnectWebSocket() |
| | | { |
| | | try |
| | | { |
| | | ClientWebSocket ws = new ClientWebSocket(); |
| | | await ws.ConnectAsync(new Uri($"ws://localhost:8888/pdm-web/daws/{LoginUser.CurrentUser?.id}"), default); |
| | | socket = ws; |
| | | ReadMsg(); |
| | | StartHeartbeat(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | await ConnectWebSocket(); |
| | | } |
| | | } |
| | | |
| | | private void ReadMsg() |
| | | { |
| | | readTask = Task.Run(() => |
| | | { |
| | | while (socket.State == WebSocketState.Open) |
| | | { |
| | | byte[] buffer = new byte[1024]; |
| | | var task = socket.ReceiveAsync(new ArraySegment<byte>(buffer), default); |
| | | task.Wait(); |
| | | var res = task.Result; |
| | | var msg = Encoding.UTF8.GetString(buffer, 0, res.Count); |
| | | HandleMsg(msg); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private void HandleMsg(string msg) |
| | | { |
| | | if (string.IsNullOrEmpty(msg) || msg == "heartcheck") |
| | | { |
| | | return; |
| | | } |
| | | Debug.WriteLine(msg); |
| | | Dispatcher.Invoke(() => |
| | | { |
| | | eDesktopAlertColor color = eDesktopAlertColor.Default; |
| | | eAlertPosition position = eAlertPosition.BottomRight; |
| | | DesktopAlert.Show(msg, "\uf005", eSymbolSet.Awesome, Color.Empty, color, position, 5, |
| | | DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), AlertClicked); |
| | | }); |
| | | } |
| | | |
| | | private void StartHeartbeat() |
| | | { |
| | | heartbeatTimer = new System.Timers.Timer(30 * 1000); |
| | | heartbeatTimer.Elapsed += HeartbeatTimer_Elapsed; |
| | | heartbeatTimer.AutoReset = true; |
| | | heartbeatTimer.Start(); |
| | | } |
| | | |
| | | private void HeartbeatTimer_Elapsed(object sender, ElapsedEventArgs e) |
| | | { |
| | | if (socket == null || socket.State != WebSocketState.Open) |
| | | { |
| | | DoDispose(); |
| | | ConnectWebSocket().Wait(); |
| | | } |
| | | else |
| | | { |
| | | byte[] buffer = Encoding.UTF8.GetBytes("test"); |
| | | socket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, default); |
| | | } |
| | | } |
| | | |
| | | public void DoDispose() |
| | | { |
| | | if (heartbeatTimer != null) |
| | | { |
| | | heartbeatTimer.Stop(); |
| | | heartbeatTimer.Dispose(); |
| | | heartbeatTimer = null; |
| | | } |
| | | if (readTask != null) |
| | | { |
| | | readTask.Dispose(); |
| | | readTask = null; |
| | | } |
| | | if (socket != null) |
| | | { |
| | | try |
| | | { |
| | | socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "DoDispose", default).Wait(); |
| | | socket.Dispose(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | |
| | | } |
| | | finally |
| | | { |
| | | socket = null; |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void Hide_Click(object sender, RoutedEventArgs e) |
| | |
| | | this.Hide(); |
| | | } |
| | | |
| | | private void Window_Deactivated(object sender, EventArgs e) |
| | | { |
| | | this.Hide(); |
| | | } |
| | | } |
| | | } |