public static async Task ConnectSocketAsync() { await Task.Run(async() => { await DispatcherHelper.ExecuteOnUIThreadAsync(async() => { try { _socket = new MessageWebSocket(); _socket.Control.MessageType = Windows.Networking.Sockets.SocketMessageType.Utf8; _socket.MessageReceived += Socket_MessageReceived; _socket.Closed += Socket_Closed; Uri uri = new Uri($"wss://im.worktile.com/socket.io/?token={DataSource.ApiUserMeData.Me.ImToken}&uid={DataSource.ApiUserMeData.Me.Uid}&client=web&EIO=3&transport=websocket"); try { await _socket.ConnectAsync(uri); if (_showConnSuccessNoti) { LightMainPage.ShowNotification("网络已恢复,已经重新连接到消息服务了。", NotificationLevel.Success, 4000); } } catch (Exception e) { LightMainPage.ShowNotification("与消息服务器断开连接,正在重新连接……", NotificationLevel.Danger, 4000); if (_reconnectTimer == null) { ReConnect(); } throw e; } using (var dataWriter = new DataWriter(_socket.OutputStream)) { string msg = $"40/message?token={DataSource.ApiUserMeData.Me.ImToken}&uid={DataSource.ApiUserMeData.Me.Uid}&client=web"; dataWriter.WriteString(msg); await dataWriter.StoreAsync(); dataWriter.DetachStream(); } KeepConnection(); if (_reconnectTimer != null) { _reconnectTimer.Cancel(); _reconnectTimer = null; } } catch { } }); }); }
public async Task ShowNotificationAsync(string text, NotificationLevel level, int duration = 0) { if (Window.Current != null && Window.Current.Content is Frame rootFrame) { if (rootFrame.Content is LightMainPage mainPage) { LightMainPage.ShowNotification(text, level, duration); return; } } var dialog = new ContentDialog { PrimaryButtonText = "关闭", DefaultButton = ContentDialogButton.Primary, Title = "异常", Content = text }; await dialog.ShowAsync(); }