예제 #1
0
 internal void LogOut()
 {
     State     = Status.Closed;
     loggedOut = true;
     Dispose();
     AVWebSocketClient.Close();
 }
예제 #2
0
 private void WebsocketClient_OnError(string obj)
 {
     PrintLog("error:" + obj);
     // 如果遇到 WebSocket 错误之后,先关闭,再按断线处理
     AVWebSocketClient.Close();
     WebsocketClient_OnClosed(0, obj, string.Empty);
 }
예제 #3
0
        /// <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, bool enforce = false, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (AVWebSocketClient.IsOpen && !enforce)
            {
                AVRealtime.PrintLog(url + "is already connectd.");
                return(Task.FromResult(true));
            }

            AVRealtime.PrintLog("websocket try to connect url :" + url + " with subprotocol: " + subprotocol);
            AVRealtime.PrintLog(url + " \tconnecting...");

            return(AVWebSocketClient.Connect(url, subprotocol));
        }
예제 #4
0
        /// <summary>
        /// 初始化实时消息客户端
        /// </summary>
        /// <param name="config"></param>
        public AVRealtime(Configuration config)
        {
            lock (mutex)
            {
                if ((int)config.OfflineMessageStrategy == 0)
                {
                    config.OfflineMessageStrategy = OfflineMessageStrategy.UnreadAck;
                }

                CurrentConfiguration = config;
                if (CurrentConfiguration.WebSocketClient != null)
                {
                    webSocketController = CurrentConfiguration.WebSocketClient;
                }
                if (CurrentConfiguration.SignatureFactory != null)
                {
                    this.SignatureFactory = CurrentConfiguration.SignatureFactory;
                }
                ReconnectOptions = new AVIMReconnectOptions()
                {
                    Interval = 5,
                    Retry    = 120
                };


                RegisterMessageType <AVIMMessage>();
                RegisterMessageType <AVIMTypedMessage>();
                RegisterMessageType <AVIMTextMessage>();
                RegisterMessageType <AVIMImageMessage>();
                RegisterMessageType <AVIMAudioMessage>();
                RegisterMessageType <AVIMVideoMessage>();
                RegisterMessageType <AVIMFileMessage>();
                RegisterMessageType <AVIMLocationMessage>();
                RegisterMessageType <AVIMRecalledMessage>();

                // 注册服务端 goaway 指令
                var goAwayListener = new GoAwayListener();
                goAwayListener.OnGoAway += () => {
                    RouterController.ClearCache().ContinueWith(_ => {
                        reborn = true;
                        // 关闭 WebSocket
                        AVWebSocketClient.Disconnect();
                    });
                };
                SubscribeNoticeReceived(goAwayListener);

                reconnectTimes = 0;
            }
        }
예제 #5
0
        /// <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, bool enforce = false, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (AVWebSocketClient.IsOpen && !enforce)
            {
                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) =>
            {
                AVWebSocketClient.OnError -= onError;

                if (tcs.Task.IsCanceled || tcs.Task.IsCompleted)
                {
                    return;
                }

                tcs.SetResult(false);

                AVRealtime.PrintLog(reason);
                //tcs.TrySetException(new AVIMException(AVIMException.ErrorCode.FromServer, "try to open websocket at " + url + "failed.The reason is " + reason, null));
            });

            Action onOpend = null;

            onOpend = (() =>
            {
                AVWebSocketClient.OnError -= onError;
                AVWebSocketClient.OnOpened -= onOpend;
                if (tcs.Task.IsCanceled || tcs.Task.IsCompleted)
                {
                    return;
                }
                tcs.SetResult(true);
                AVRealtime.PrintLog(url + " connected.");
            });

            Action <int, string, string> onClosed = null;

            onClosed = (reason, arg0, arg1) =>
            {
                AVWebSocketClient.OnError  -= onError;
                AVWebSocketClient.OnOpened -= onOpend;
                AVWebSocketClient.OnClosed -= onClosed;
                if (tcs.Task.IsCanceled || tcs.Task.IsCompleted)
                {
                    return;
                }
                tcs.SetResult(true);
                //tcs.TrySetException(new AVIMException(AVIMException.ErrorCode.FromServer, "try to open websocket at " + url + "failed.The reason is " + reason, null));
            };

            AVWebSocketClient.OnOpened += onOpend;
            AVWebSocketClient.OnClosed += onClosed;
            AVWebSocketClient.OnError  += onError;
            AVWebSocketClient.Open(url, subprotocol);

            return(tcs.Task);
        }