Close() public method

public Close ( ) : void
return void
Ejemplo n.º 1
0
 public void close()
 {
     dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
     {
         try
         {
             state = WebSocketState.CLOSING;
             sck.Close(0, "Closed");
             closeSocket();
             state = WebSocketState.CLOSED;
         } catch (Exception ex)
         {
             com.codename1.io.websocket.WebSocket.errorReceived(id, ex.Message, ex.HResult);
         }
     });
 }
Ejemplo n.º 2
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            taskInstance.Canceled += OnCanceled;

            _deferral = taskInstance.GetDeferral();
            _taskInstance = taskInstance;

            //var messageReceiver = new TextSecureMessageReceiver(PUSH_URL, TextSecurePreferences.getLocalNumber(), TextSecurePreferences.getPushServerPassword(), TextSecurePreferences.getSignalingKey(), "Test User Agent");
            //var connection = new WebSocketConnection(PUSH_URL, TextSecurePreferences.getLocalNumber(), TextSecurePreferences.getPushServerPassword(), "Test User Agent");
            //connection.Connected += OnConnected;
            var username = TextSecurePreferences.getLocalNumber();
            var password = TextSecurePreferences.getPushServerPassword();
            var userAgent = "ASdfA";

            try
            {
                socket = new MessageWebSocket();
                if (userAgent != null) socket.SetRequestHeader("X-Signal-Agent", userAgent);
                socket.MessageReceived += OnMessageReceived;
                socket.Closed += OnClosed;

                var wsUri = PUSH_URL.Replace("https://", "wss://")
                                              .Replace("http://", "ws://") + $"/v1/websocket/?login={username}&password={password}";
                Uri server = new Uri(wsUri);
                await socket.ConnectAsync(server);
                Debug.WriteLine("WebsocketTask connected...");
                keepAliveTimer = new Timer(sendDisconnect, null, TimeSpan.FromSeconds(15), Timeout.InfiniteTimeSpan);


                //messageWriter = new DataWriter(socket.OutputStream);



            }
            catch (Exception e)
            {
                WebErrorStatus status = WebSocketError.GetStatus(e.GetBaseException().HResult);

                switch (status)
                {
                    case WebErrorStatus.CannotConnect:
                    case WebErrorStatus.NotFound:
                    case WebErrorStatus.RequestTimeout:
                        Debug.WriteLine("Cannot connect to the server. Please make sure " +
                            "to run the server setup script before running the sample.");
                        break;

                    case WebErrorStatus.Unknown:
                        throw;

                    default:
                        Debug.WriteLine("Error: " + status);
                        break;
                }

                Debug.WriteLine("fuuuu");
                socket.Close(1000, "None");
            }

            //var pipe = messageReceiver.createMessagePipe();
            // pipe.MessageReceived += OnMessageRecevied;

        }
Ejemplo n.º 3
0
        private async Task OpenWebSocketAsync()
        {
            _closeTaskCompletionSource = new TaskCompletionSource<object>();

            try
            {
                var webSocketUrl = (await GetApiInfoAsync()).WebSocketServerUrl + "/client";

                _webSocket = new MessageWebSocket();
                _webSocket.Control.MessageType = SocketMessageType.Utf8;
                _webSocket.MessageReceived += (s, e) => Task.Run(() => HandleMessage(e));
                _webSocket.Closed += (s, e) => Task.Run(() => HandleConnectionClose());
                await _webSocket.ConnectAsync(new Uri(webSocketUrl));

                _socketWriter = new DataWriter(_webSocket.OutputStream);

                await AuthenticateAsync();

                SetChannelState(ChannelState.Connected);
            }
            catch
            {
                try
                {
                    if (_webSocket != null)
                    {
                        _webSocket.Close(1000, "Abnormal Closure");
                    }
                }
                catch { }
                finally
                {
                    if (_webSocket != null)
                    {
                        _webSocket.Dispose();
                        _webSocket = null;
                    }
                    SetChannelState(ChannelState.Disconnected);
                }
                throw;
            }
        }
Ejemplo n.º 4
0
 public void Close()
 {
     m_Socket.Close(1000, "Closed") ;
 }