예제 #1
0
        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);
        }
예제 #2
0
        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.");
        }
예제 #3
0
        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");
            }
        }
예제 #4
0
파일: Bootstrap.cs 프로젝트: lulzzz/WCloud
        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);
        }