SetRequestHeader() публичный Метод

public SetRequestHeader ( [ headerName, [ headerValue ) : void
headerName [
headerValue [
Результат void
        public async void connect()
        {
            Debug.WriteLine("WSC connect()...");

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

                try
                {
                    Uri server = new Uri(wsUri);
                    await socket.ConnectAsync(server);
                    //Connected(this, EventArgs.Empty);
                    keepAliveTimer = new Timer(sendKeepAlive, null, TimeSpan.FromSeconds(KEEPALIVE_TIMEOUT_SECONDS), TimeSpan.FromSeconds(KEEPALIVE_TIMEOUT_SECONDS));

                    
                    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("WSC connected...");
            }
        }
        public void Open(string url, string protocol = null, string authToken = null)
        {
            try
            {
                if (_websocket != null)
                    EndConnection();

                _websocket = new MessageWebSocket();
                _websocket.Control.MessageType = SocketMessageType.Utf8;
                _websocket.Closed += _websocket_Closed;
                _websocket.MessageReceived += _websocket_MessageReceived;

                if (url.StartsWith("https"))
                    url = url.Replace("https://", "wss://");
                else if (url.StartsWith("http"))
                    url = url.Replace("http://", "ws://");

                if (authToken != null)
                {
                    _websocket.SetRequestHeader("Authorization", authToken);
                }

                _websocket.ConnectAsync(new Uri(url)).Completed = (source, status) =>
                {
                    if (status == AsyncStatus.Completed)
                    {
                        messageWriter = new DataWriter(_websocket.OutputStream);
                        IsOpen = true;
                        OnOpened();
                    }
                    else if (status == AsyncStatus.Error)
                    {
                        OnError("Websocket error");
                    }
                };


            }
            catch (Exception ex)
            {
                OnError(ex.Message);
            }
        }
Пример #3
0
        public async Task ConnectAsync(Uri uri, CancellationToken cancellationToken, ClientWebSocketOptions options)
        {
            InterlockedCheckAndUpdateState(WebSocketState.Connecting, s_validConnectStates);
            CheckValidState(s_validConnectingStates);

            _messageWebSocket = new MessageWebSocket();
            foreach (var header in options.RequestHeaders)
            {
                _messageWebSocket.SetRequestHeader((string)header, options.RequestHeaders[(string)header]);
            }

            string cookies = options.Cookies == null ? null : options.Cookies.GetCookieHeader(uri);
            if (!string.IsNullOrEmpty(cookies))
            {
                _messageWebSocket.SetRequestHeader(HeaderNameCookie, cookies);
            }

            var websocketControl = _messageWebSocket.Control;
            foreach (var subProtocol in options.RequestedSubProtocols)
            {
                websocketControl.SupportedProtocols.Add(subProtocol);
            }

            try
            {
                _receiveAsyncBufferTcs = new TaskCompletionSource<ArraySegment<byte>>();
                _closeWebSocketReceiveResultTcs = new TaskCompletionSource<WebSocketReceiveResult>();
                _messageWebSocket.MessageReceived += OnMessageReceived;
                _messageWebSocket.Closed += OnCloseReceived;
                await _messageWebSocket.ConnectAsync(uri).AsTask(cancellationToken);
                _subProtocol = _messageWebSocket.Information.Protocol;
                _messageWriter = new DataWriter(_messageWebSocket.OutputStream);
            }
            catch (Exception)
            {
                UpdateState(WebSocketState.Closed);
                throw;
            }

            UpdateState(WebSocketState.Open);
        }
Пример #4
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;

        }