/// <summary> /// 打开 WebSocket 链接 /// </summary> /// <param name="wss">websocket server address</param> /// <param name="cancellationToken"></param> /// <returns></returns> internal Task OpenAsync(string wss, CancellationToken cancellationToken = default(CancellationToken)) { AVRealtime.PrintLog(wss + " connecting..."); var tcs = new TaskCompletionSource <bool>(); Action <string> onError = null; onError = ((reason) => { PCLWebsocketClient.OnError -= onError; tcs.SetResult(false); tcs.TrySetException(new AVIMException(AVIMException.ErrorCode.FromServer, "try to open websocket at " + wss + "failed.The reason is " + reason, null)); }); Action onOpend = null; onOpend = (() => { PCLWebsocketClient.OnError -= onError; PCLWebsocketClient.OnOpened -= onOpend; tcs.SetResult(true); AVRealtime.PrintLog(wss + " connected."); }); PCLWebsocketClient.OnOpened += onOpend; PCLWebsocketClient.OnError += onError; PCLWebsocketClient.Open(wss); return(tcs.Task); }
public void KeepAlive() { if (this._heartBeatingToggle) { PCLWebsocketClient.Send(this._beatPacket); } }
internal void LogOut() { this.State = Status.Offline; ToggleNotification(false); m_NoticeReceived = null; m_OnDisconnected = null; PCLWebsocketClient.Close(); }
/// <summary> /// open webcoket connection with cloud. /// </summary> /// <param name="url">wss address</param> /// <param name="subprotocol">subprotocol for websocket</param> /// <param name="cancellationToken"></param> /// <returns></returns> public Task <bool> OpenAsync(string url, string subprotocol = null, CancellationToken cancellationToken = default(CancellationToken)) { if (PCLWebsocketClient.IsOpen) { AVRealtime.PrintLog(url + "is already connectd."); return(Task.FromResult(true)); } AVRealtime.PrintLog("websocket try to connect url :" + url + "with subprotocol: " + subprotocol); AVRealtime.PrintLog(url + " connecting..."); var tcs = new TaskCompletionSource <bool>(); Action <string> onError = null; onError = ((reason) => { PCLWebsocketClient.OnError -= onError; tcs.TrySetResult(false); tcs.TrySetException(new AVIMException(AVIMException.ErrorCode.FromServer, "try to open websocket at " + url + "failed.The reason is " + reason, null)); }); Action onOpend = null; onOpend = (() => { PCLWebsocketClient.OnError -= onError; PCLWebsocketClient.OnOpened -= onOpend; tcs.TrySetResult(true); AVRealtime.PrintLog(url + " connected."); }); Action <int, string, string> onClosed = null; onClosed = (reason, arg0, arg1) => { PCLWebsocketClient.OnError -= onError; PCLWebsocketClient.OnOpened -= onOpend; PCLWebsocketClient.OnClosed -= onClosed; tcs.TrySetResult(false); tcs.TrySetException(new AVIMException(AVIMException.ErrorCode.FromServer, "try to open websocket at " + url + "failed.The reason is " + reason, null)); }; PCLWebsocketClient.OnOpened += onOpend; PCLWebsocketClient.OnClosed += onClosed; PCLWebsocketClient.OnError += onError; PCLWebsocketClient.Open(url, subprotocol); return(tcs.Task); }
internal void LogOut() { State = Status.Closed; Dispose(); PCLWebsocketClient.Close(); }