public static UserRegistrationInfo ToRegInfo(this WsConnection con) { var res = new UserRegistrationInfo() { UserID = con.Client.SubjectID, DeviceType = con.Client.DeviceType, Payload = new UserRegistrationInfoPayload() { ServerInstanceID = con.Server.ServerInstanceID, PingTimeUtc = DateTime.UtcNow } }; return(res); }
public override async Task OnConnectedAsync() { await base.OnConnectedAsync(); try { var cancellation = new CancellationTokenSource(); var wsConnection = new WsConnection(Clients.Client(Context.ConnectionId)); var comunicationManager = _serviceFactory.GetInstance <ResilientCommunicationManager>(); _connections.TryAdd(Context.ConnectionId, comunicationManager); await comunicationManager.Start(wsConnection, cancellation); } catch (Exception e) { _logger.LogError(e, "A fatal error has occurred."); } _logger.LogInformation("Execution ended."); }
private void ProcessWsMessage(string json) { if (WsConnection.IsJson(json)) { var parameterChangedTask = Task.Run(() => { var parameterSet = json.TryDeserializeObject <ParameterValueUpdateList>(); if (parameterSet != null && parameterSet.Count > 0) { foreach (var parameterEntry in parameterSet.Items) { OnParameterValueChanged(new ParameterValueChangedEventArgs(parameterEntry.NodeId, parameterEntry.Index, parameterEntry.SubIndex, parameterEntry.ParameterValue)); } } else { var parameterEntry = json.TryDeserializeObject <ParameterValueUpdate>(); if (parameterEntry != null) { OnParameterValueChanged(new ParameterValueChangedEventArgs(parameterEntry.NodeId, parameterEntry.Index, parameterEntry.SubIndex, parameterEntry.ParameterValue)); } } }); lock (this) { _parameterChangedTasks.Add(parameterChangedTask); } } else { MsgLogger.WriteDebug($"{GetType().Name} - Execute", $"unknown message '{json}' received"); } }
public static IApplicationBuilder UseWebSocketEndpoint <T>(this IApplicationBuilder app, string path) where T : IWsServer { app.Use(async(context, next) => { if (context.Request.Path == path) { if (context.WebSockets.IsWebSocketRequest) { var me = context.Request.Query["me"]; var device = context.Request.Query["device"]; var webSocket = await context.WebSockets.AcceptWebSocketAsync(); var client = new WsClient(webSocket) { SubjectID = me, DeviceType = device, ConnectionID = context.Connection.Id }; var server = context.RequestServices.Resolve_ <T>(); var connection = new WsConnection(context.RequestServices, server, client); await connection.StartReceiveMessageLoopAsync(CancellationToken.None); } else { context.Response.StatusCode = 400; } } else { await next(); } }); app.ApplicationServices.Resolve_ <T>().Start(); return(app); }