Пример #1
0
        protected async Task <MazeServer> GetServerConnection()
        {
            if (_mazeServer != null)
            {
                return(_mazeServer);
            }

            await _mazeServerLock.WaitAsync();

            try
            {
                if (_mazeServer != null)
                {
                    return(_mazeServer);
                }

                var builder = new UriBuilder(_httpClient.BaseAddress)
                {
                    Path = "ws", Scheme = _httpClient.BaseAddress.Scheme == "https" ? "wss" : "ws"
                };

                var connector = new MazeSocketConnector(builder.Uri)
                {
                    AuthenticationHeaderValue = _httpClient.DefaultRequestHeaders.Authorization
                };
                var dataStream = await connector.ConnectAsync();

                var webSocket = WebSocket.CreateClientWebSocket(dataStream, null, 8192, 8192, TimeSpan.FromMinutes(2), true,
                                                                WebSocket.CreateClientBuffer(8192, 8192));

                var webSocketWrapper = new WebSocketWrapper(webSocket, 8192);
                _mazeServer = new MazeServer(webSocketWrapper, 8192, 4096, ArrayPool <byte> .Shared);

                webSocketWrapper.ReceiveAsync().ContinueWith(ReceiveAsyncContinuation).Forget();
                return(_mazeServer);
            }
            finally
            {
                _mazeServerLock.Release();
            }
        }
Пример #2
0
        public async Task InitializeWebSocket(ILifetimeScope lifetimeScope)
        {
            var builder = new UriBuilder(RestClient.BaseUri)
            {
                Path   = "ws",
                Scheme = RestClient.BaseUri.Scheme == "https" ? "wss" : "ws"
            };

            var connector = new MazeSocketConnector(builder.Uri)
            {
                AuthenticationHeaderValue = new AuthenticationHeaderValue("Bearer", RestClient.Jwt)
            };
            var dataStream = await connector.ConnectAsync();

            var webSocket = WebSocket.CreateClientWebSocket(dataStream, null, _options.PackageBufferSize, _options.PackageBufferSize,
                                                            _options.KeepAliveInterval, true, WebSocket.CreateClientBuffer(_options.PackageBufferSize, _options.PackageBufferSize));

            var wrapper = new WebSocketWrapper(webSocket, _options.PackageBufferSize);
            var server  = new MazeServer(wrapper, _options.PackageBufferSize, _options.MaxHeaderSize, ArrayPool <byte> .Shared);

            Listener = new ServerCommandListener(connector, wrapper, server, lifetimeScope);
            await Listener.Listen();
        }