Exemple #1
0
        public WebSocketIO Connect(ConnectionCallback onSuccess, ConnectionCallbackError onError)
        {
            m_webSocket.OnOpen    += EventHandlerOnOpen;
            m_webSocket.OnClose   += EventHandlerOnClose;
            m_webSocket.OnMessage += EventHandlerOnMessage;
            m_webSocket.OnError   += EventHandlerOnError;

            m_webSocket.OnOpen  += OnOpen;
            m_webSocket.OnClose += OnClose;
            m_webSocket.Connect();
            return(this);

            void OnOpen(object sender, EventArgs e)
            {
                m_webSocket.OnOpen  -= OnOpen;
                m_webSocket.OnClose -= OnClose;
                onSuccess?.Invoke();
            }

            void OnClose(object sender, CloseEventArgs e)
            {
                m_webSocket.OnOpen  -= OnOpen;
                m_webSocket.OnClose -= OnClose;
                onError?.Invoke(e.Code, e.Reason, e.WasClean);
            }
        }
        public void connect(ConnectionCallback onComplete)
        {
            CallbackState state = new CallbackState(socket, onComplete);

            try
            {
                socket.BeginConnect(ipEndPoint, ConnectCallback, state);
            }
            //catch (ArgumentNullException ae)
            //{
            //    Console.WriteLine("ArgumentNullException : {0}", ae.ToString());
            //}
            //catch (SocketException se)
            //{
            //    Console.WriteLine("SocketException : {0}", se.ToString());
            //}
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
                var connectionResult = new ConnectionResult();
                connectionResult.result  = ConnectionResult.Result.FAILURE;
                connectionResult.Message = e.Message;
                onComplete.Invoke(connectionResult);
            }
        }
Exemple #3
0
        public async Task OpenAsync(ConnectionCallback callback = null)
        {
            try
            {
                await socket.ConnectTaskAsync(RemoteEndPoint).ConfigureAwait(false);

                IsConnected = true;
            }
            catch (SocketException ex)
            {
                IsConnected = false;
                throw new CommunicationException("Подключение не установлено.", ex);
            }

            if (callback != null)
            {
                await callback.Invoke().ConfigureAwait(false);
            }
        }