Exemplo n.º 1
0
        protected override async Task <IMicroClient> Create(ServiceAddress address)
        {
            var listener  = new MessageListener();
            var webSocket = new ClientWebSocket();
            var uri       = new Uri(new Uri(address.ToString()), "/micro/ws");
            await webSocket.ConnectAsync(uri, CancellationToken.None);

            var codec  = Provider.GetClientCodec(address.Codec);
            var sender = new WebSocketMessageSender(webSocket, codec, address.Gzip);

            var completion   = new TaskCompletionSource <object>();
            var socketClient = new WebSocketClient(webSocket, LoggerFactory, completion);

            socketClient.OnReceive += async buffer =>
            {
                var resultMessage = await codec.DecodeAsync <MessageResult>(buffer, address.Gzip);

                await listener.OnReceived(sender, resultMessage);
            };
            socketClient.OnClose += (key, socket) => Remove(address);

            _ = Task.Run(() => socketClient.ReceiveAsync(CancellationToken.None).ConfigureAwait(false));
            return(new MicroClient(sender, listener, MicroExecutor, LoggerFactory));
        }