コード例 #1
0
        public async Task Connect(string remote, WebSocketConnectionOptions options = null)
        {
            _connection = new WebSocketConnection();
            await _connection.Connect(remote, options);

            AsyncWorker.Run(EventLoop);
            await TriggerConnected();
        }
コード例 #2
0
        public async Task Connect(string remote, WebSocketConnectionOptions options = null)
        {
            if (_state != WebSocketConnectionState.Closed)
            {
                throw new Exception($"Invalid state transition {_state} -> {WebSocketConnectionState.Connecting}");
            }

            if (options == null)
            {
                options = new WebSocketConnectionOptions();
            }

            _state   = WebSocketConnectionState.Connecting;
            _options = options;

            // fix urls with no prefix
            if (!remote.StartsWith("ws://"))
            {
                remote = $"ws://{remote}";
            }

            _client = new ClientWebSocket()
            {
                Options =
                {
                    KeepAliveInterval = _options.KeepAliveInterval
                }
            };

            using (var cts = new CancellationTokenSource(_options.ConnectionTimeout))
            {
                await _client.ConnectAsync(new Uri(remote), cts.Token);
            }

            _state = WebSocketConnectionState.Open;
        }