Ejemplo n.º 1
0
        public async Task InvokeAsync(WebSocketMessageContext context)
        {
            if (context.Command == WebSocketCommands.Handshake)
            {
                var connectionId = context.Value.ToString();
                await _connector.SendAsync(new WebSocketMessageContext
                {
                    Command     = WebSocketCommands.DataSend,
                    MessageType = WebSocketMessageType.Text,
                    Value       = connectionId,
                    Header      = new RouteValueDictionary(new { LayoutRequest = "layoutrequest" })
                });
            }

            if (context.MessageType == WebSocketMessageType.Binary)
            {
                if (context.Header.TryGetValue("fileupdated", out object fullname))
                {
                    var pageContent = context.Value?.ToString();
                    using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(pageContent)))
                    {
                        var fileFullName = fullname.ToString();
                        var name         = Path.GetFileName(fileFullName);
                        _cliFileProvider.Layout = new InMemoryFileInfo(name, fileFullName, memoryStream.ToArray(), DateTime.UtcNow);
                        _cliFileProvider.RaiseChange(fileFullName);
                    }
                }
            }

            await Task.CompletedTask;
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> KeepAlive()
        {
            await _connector.SendAsync(new WebSocketMessageContext
            {
                Command = WebSocketCommands.DataSend,
                Value   = new { Id = 1, Name = "Hello World!", DateTime = DateTime.Now }
            });

            return(Ok());
        }
 private async Task Send <T>(T @object, CancellationToken cancellationToken = default)
 {
     var json = JsonSerializer.SerializeObject(@object);
     await webSocketConnector.SendAsync(json, cancellationToken);
 }