Beispiel #1
0
        private async Task Echo(HttpContext context, WebSocket webSocket, ICBRRepository cbr)
        {
            var buffer = new byte[1024 * 4];
            WebSocketReceiveResult result = null;

            try
            {
                result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);

                while (!result.CloseStatus.HasValue)
                {
                    var arrayReceiveResult = new ArraySegment <byte>(buffer, 0, result.Count);
                    var incomingMessage    = Encoding.Default.GetString(arrayReceiveResult.Array);
                    incomingMessage = incomingMessage.Substring(0, result.Count);
                    if (!string.IsNullOrEmpty(incomingMessage))
                    {
                        var dict = await cbr.GetAsync();

                        var rate            = dict != null && dict.ContainsKey(incomingMessage) ? dict[incomingMessage] : null;
                        var rateString      = JsonSerializer.Serialize(rate);
                        var arraySendResult = Encoding.Default.GetBytes(rateString);
                        await webSocket.SendAsync(new ArraySegment <byte>(arraySendResult, 0, (int)arraySendResult.Length), result.MessageType, result.EndOfMessage, CancellationToken.None);

                        result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "WebSocket");
            }
            if (result != null)
            {
                await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
            }
        }
Beispiel #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ICBRRepository cbr)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            else
            {
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseCors(cors);
            app.UseMvc();

            app.UseWebSockets();
            app.Use(async(context, next) =>
            {
                if (context.Request.Path == "/ws")
                {
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
                        await Echo(context, webSocket, cbr);
                    }
                    else
                    {
                        context.Response.StatusCode = 400;
                    }
                }
                else
                {
                    await next();
                }
            });
        }
Beispiel #3
0
 public RateExchangeController(ILogger <RateExchangeController> _logger, ICBRRepository _cbrRepo)
 {
     logger        = _logger;
     cBRRepository = _cbrRepo;
 }