/// <summary> /// Handles long polling requests /// </summary> /// <param name="context">HttpContext</param> /// <param name="options">Middleware options</param> /// <returns></returns> public async Task HandleRequest(HttpContext context, LWMessagePushMiddlewareOptions options) { try { var startDateTime = DateTime.UtcNow; var endDateTime = startDateTime.AddSeconds(options.LongPollingConfiguration.TimeoutSeconds); string topic = context.Request.Query["topic"]; string lastReceive = context.Request.Query["lastr"]; options.LogListener.Log(string.Format("LongPolling request is received at {0} UTC for {1}", startDateTime.ToString(), topic), Logging.LogLevel.Verbose); if (topic == null) { string err = "Topic is required."; options.LogListener.Log(string.Format("LongPolling handler error: {0}", err), Logging.LogLevel.Error); throw new ArgumentNullException(err); } DateTime lastReceivedMsgDT = DateTime.UtcNow; if (lastReceive != null) { DateTime.TryParse(lastReceive, out lastReceivedMsgDT); } List <PushMessage> messageList = Internal_HandleRequest(topic, endDateTime, lastReceivedMsgDT); await context.Response.WriteAsync(Newtonsoft.Json.JsonConvert.SerializeObject(messageList)); } catch (Exception e) { options.LogListener.Log(string.Format("LongPolling handler error: {0}", e.Message), Logging.LogLevel.Error); throw e; } }
/// <summary> /// Ctor /// </summary> /// <param name="next"></param> /// <param name="builder"></param> /// <param name="options"></param> public LWMessagePushMiddleware(RequestDelegate next, IApplicationBuilder builder, LWMessagePushMiddlewareOptions options) { _next = next; _options = options; _connectionHandlerFactory = (IConnectionHandlerFactoryService)builder.ApplicationServices.GetService(typeof(IConnectionHandlerFactoryService)); _clientCode = null; }
/// <summary> /// Handles web socket requests /// </summary> /// <param name="context">HttpContext</param> /// <param name="options">Middleware options</param> /// <returns></returns> public async Task HandleRequest(HttpContext context, LWMessagePushMiddlewareOptions options) { try { if (context.WebSockets.IsWebSocketRequest) { if (!context.Request.Query.ContainsKey("topic")) { string err = "Topic is required."; options.LogListener.Log(string.Format("WebSocket handler error: {0}", err), Logging.LogLevel.Error); throw new ArgumentNullException(err); } string topic = context.Request.Query["topic"]; Guid socketId = Guid.NewGuid(); string lastReceive = context.Request.Query["lastr"]; DateTime lastReceivedMsgDT = DateTime.UtcNow; options.LogListener.Log(string.Format("WebSocket connection request is received at {0} UTC for {1}", lastReceivedMsgDT.ToString(), topic), Logging.LogLevel.Verbose); if (lastReceive != null) { DateTime.TryParse(lastReceive, out lastReceivedMsgDT); } WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); WebSocketItemInfo socketInfo = new WebSocketItemInfo(webSocket, lastReceivedMsgDT); var newTopicDict = new ConcurrentDictionary <Guid, WebSocketItemInfo>(); newTopicDict.TryAdd(socketId, socketInfo); _sockets.AddOrUpdate(topic, newTopicDict, (a, b) => { b.TryAdd(socketId, socketInfo); return(b); }); await ProcessRequest(context, socketId, socketInfo, topic); } else { context.Response.StatusCode = 400; } } catch (Exception e) { options.LogListener.Log(string.Format("WebSocket handler error: {0}", e.Message), Logging.LogLevel.Error); throw e; } }
/// <summary> /// Adds LWMessagePush middleware to the pipeline /// </summary> /// <param name="builder">IApplicationBuilder instance</param> /// <param name="options">LWMessagePush middleware options</param> /// <returns></returns> public static IApplicationBuilder UseLWMessagePush(this IApplicationBuilder builder, LWMessagePushMiddlewareOptions options = null) { // Default configuration if (options == null) { options = new LWMessagePush.Options.LWMessagePushMiddlewareOptions() { LongPollingConfiguration = new LWMessagePush.Options.LWMessagePushLongPollingOptions() { TimeoutSeconds = 10 }, WebSocketConfigurations = new LWMessagePush.Options.LWMessagePushWebSocketOptions() { KeepAliveInterval = TimeSpan.FromSeconds(120), ReceiveBufferSize = 4 * 1024 } } } ; if (options.WebSocketConfigurations == null) { throw new ArgumentNullException(nameof(options.WebSocketConfigurations)); } if (options.LongPollingConfiguration == null) { throw new ArgumentNullException(nameof(options.LongPollingConfiguration)); } builder.UseWebSockets(options.WebSocketConfigurations); return(builder.UseMiddleware <LWMessagePushMiddleware>(builder, options)); }