public async Task InvokeAsync(WebSocketMessageContext context)
        {
            if (context.MessageType == WebSocketMessageType.Text &&
                context.Command == WebSocketCommands.DataSend)
            {
                if (context.Header != null)
                {
                    if (context.Header.TryGetValue("layoutrequest", out object layoutrequest))
                    {
                        if (!string.IsNullOrEmpty(HostingHelper.MainAppDirectory))
                        {
                            var layoutPagePath = PathUtility.GetLayoutPagePath(HostingHelper.MainAppDirectory);
                            var pageContent    = File.ReadAllText(layoutPagePath);
                            await _connectionManager.BroadcastAsyncFileChanged(pageContent, layoutPagePath);

                            return;
                        }

                        using (var scope = _applicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
                            using (var db = scope.ServiceProvider.GetService <HisarCliContext>())
                            {
                                var page = db.Set <Page>().FirstOrDefault(x => x.PageType == PageType.Layout);
                                await _connectionManager.BroadcastAsyncFileChanged(page.Content, page.Name);
                            }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public async Task InvokeAsync(WebSocketMessageContext context)
        {
            if (context.MessageType == WebSocketMessageType.Text)
            {
            }

            if (context.MessageType == WebSocketMessageType.Binary)
            {
                var header = context.Header as Dictionary <string, object>;
                if (header != null)
                {
                    object key = null;
                    if (header.TryGetValue(WebSocketHeaderNames.CacheItemKey, out key))
                    {
                        await Task.Run(() => _cacheProvider.SetObject(key.ToString(),
                                                                      context.Value,
                                                                      new MemoryCacheEntryOptions {
                            Priority = CacheItemPriority.NeverRemove
                        }));
                    }
                }
                var    length = context.Length;
                double size   = (length / 1024f) / 1024f;
                context.Value = $"{size} MB <<binary>>";
            }

            // Sending incoming data from Backend zone to the Clients (Browsers)
            await _connectionManager.BroadcastAsync(context);
        }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
0
        public async Task <ApiResult> Yaz([FromBody] DefterGenericMessage model)
        {
            if (model == null)
            {
                return(new ApiResult());
            }

            try
            {
                // Burada oluşabilecek hatayı yakalayıp explicit 500 dönüyor ve API Gateway' in logu kendi
                // veri tabanına yazmasını sağlıyoruz.
                _logManager.SaveLog(model.ConvertToDefterLog());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                var webSocketContext = new WebSocketMessageContext {
                    Command = WebSocketCommands.DataSend, Value = model
                };
                _messageQueue.Enqueue(webSocketContext);
            }

            await Task.CompletedTask;

            return(new ApiResult());
        }
Exemplo n.º 5
0
        public async Task InvokeAsync(WebSocketMessageContext context)
        {
            if (context.Command == WebSocketCommands.Connect)
            {
            }

            // Sending incoming data from Backend zone to the Clients (Browsers)
            await _connectionManager.BroadcastAsync(context);
        }
        public async Task <IActionResult> SendAsync([FromBody] SimpleCacheItem model)
        {
            var echo             = $"Echo from server '{model.Id}' '{model.Name}' - {DateTime.Now}";
            var obj              = new { message = echo };
            var webSocketContext = new WebSocketMessageContext {
                Command = WebSocketCommands.DataSend, Value = obj
            };
            await _connectionManager.BroadcastAsync(webSocketContext);

            return(Ok());
        }
Exemplo n.º 7
0
        public IActionResult Broadcast([FromBody] BroadcastMessageContext context)
        {
            var messageContext = new WebSocketMessageContext
            {
                Command = WebSocketCommands.DataSend,
                Value   = context
            };

            _connectionManager.BroadcastAsync(messageContext);
            return(Json(messageContext));
        }
        public async Task CacheReady()
        {
            var webSocketContext = new WebSocketMessageContext {
                Command = WebSocketCommands.DataSend,
                Header  = new RouteValueDictionary(new { CacheReady = Globals.CacheReady }),
                Value   = new Context
                {
                    Keys = new string[] { nameof(SimpleCacheItem) }
                }
            };

            await _connectionManager.BroadcastAsync(webSocketContext);
        }
        public async Task InvokeAsync(WebSocketMessageContext context)
        {
            await Task.CompletedTask;

            if (context.MessageType == WebSocketMessageType.Text)
            {
                await _connectionManager.BroadcastAsync(context);

                return;
            }

            if (context.MessageType == WebSocketMessageType.Binary)
            {
            }
        }
        public async Task InvokeAsync(WebSocketMessageContext context)
        {
            if (context.MessageType == WebSocketMessageType.Text)
            {
            }

            if (context.MessageType == WebSocketMessageType.Binary)
            {
                if (context.Header is Dictionary <string, object> stateDictionary)
                {
                }
            }

            await _connectionManager.BroadcastAsync(context);
        }
Exemplo n.º 11
0
 public async Task InvokeAsync(WebSocketMessageContext context)
 {
     if (context.MessageType == WebSocketMessageType.Text &&
         context.Command == WebSocketCommands.DataSend)
     {
         if (context.Header != null)
         {
             object cacheRequest = null;
             if (context.Header.TryGetValue(nameof(WebSocketHeaderNames.CacheRequest), out cacheRequest))
             {
                 var connectionId = context.Value.ToString();
                 await _cache.SendCache(_connectionManager, connectionId);
             }
         }
     }
 }
        public async Task InvokeAsync(WebSocketMessageContext context)
        {
            await Task.CompletedTask;

            if (context.MessageType == WebSocketMessageType.Text)
            {
                await _connectionManager.BroadcastAsync(context);

                return;
            }

            if (context.MessageType == WebSocketMessageType.Binary)
            {
                var stateDictionary = context.Header as Dictionary <string, object>;
                if (stateDictionary != null)
                {
                    object key = null;
                    if (stateDictionary.TryGetValue("Key", out key))
                    {
                        var keyStr     = key.ToString();
                        var descriptor = CacheHelper.GetDescriptor(key.ToString());
                        try
                        {
                            var genericList = descriptor.Type.CreateElementTypeAsGenericList();
                            var value       = context.Value;
                            _cacheProvider.SetObject(keyStr, value, new MemoryCacheEntryOptions {
                                Priority = CacheItemPriority.NeverRemove
                            });

#if DEBUG
                            var length  = context.Length;
                            var message = $"===Sandbox: {Environment.MachineName}===Key: {keyStr}===Length: {length}===";
                            _logger.LogDebug(message);
#endif
                        }
                        catch (Exception ex)
                        {
#if DEBUG
                            var length  = context.Length;
                            var message = $"===Exception: {ex.Message}===Sandbox: {Environment.MachineName}===Key: {keyStr}===Length: {length}===";
                            _logger.LogError(message);
#endif
                        }
                    }
                }
            }
        }
        public async Task InvokeAsync(WebSocketMessageContext context)
        {
            await Task.CompletedTask;

            if (context.Command == WebSocketCommands.DataSend)
            {
                object cacheReady = null;
                if (context.Header.TryGetValue(nameof(Globals.CacheReady), out cacheReady))
                {
                    Interlocked.Increment(ref ApplicationVariables.CacheReady);
                }
            }

            if (context.MessageType == WebSocketMessageType.Binary)
            {
                // noop
            }
        }
Exemplo n.º 14
0
        public async Task <IActionResult> SendTextAsync([FromBody] SimpleModel model)
        {
            if (model != null)
            {
                var echo             = $"Echo from server '{model.Key}' - {DateTime.Now}";
                var obj              = new { message = echo };
                var webSocketContext = new WebSocketMessageContext {
                    Command = WebSocketCommands.DataSend, Value = obj
                };

                var str = JsonConvert.SerializeObject(webSocketContext, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });

                var bytes = Encoding.UTF8.GetBytes(str);
                await _connectionManager.BroadcastAsync(bytes);
            }

            return(Ok());
        }
Exemplo n.º 15
0
        private ArraySegment <byte> CreateTextSegment(WebSocketMessageContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            object connectionId = string.Empty;

            if (context.Header.TryGetValue(NCSConstants.ConnectionId, out connectionId))
            {
                var id = connectionId as string;
                if (string.IsNullOrEmpty(id))
                {
                    throw new InvalidOperationException(nameof(connectionId));
                }
            }
            else
            {
                context.Header.Add(NCSConstants.ConnectionId, ConnectionId);
            }

            return(context.ToSegment());
        }
 public async Task InvokeAsync(WebSocketMessageContext context)
 {
     await Task.CompletedTask;
 }
        public async Task InvokeAsync(WebSocketMessageContext context)
        {
            await Task.CompletedTask;

            Debug.WriteLine("Context length: {0}", context.Length);
        }
Exemplo n.º 18
0
 public async Task SendAsync(WebSocketMessageContext context)
 {
     var segments = CreateTextSegment(context);
     await _webSocket.SendAsync(segments, WebSocketMessageType.Text, true, CancellationToken.None);
 }
Exemplo n.º 19
0
 public void Enqueue(WebSocketMessageContext context)
 {
     _socketQueue.Enqueue(context);
 }
Exemplo n.º 20
0
 public Task InvokeAsync(WebSocketMessageContext context)
 {
     // Sending incoming data from backend to the Browser(s)
     _connectionManager.BroadcastAsync(context);
     return(Task.CompletedTask);
 }
Exemplo n.º 21
0
 public async Task InvokeAsync(WebSocketMessageContext context)
 {
     await _connectionManager.BroadcastAsync(context);
 }
Exemplo n.º 22
0
 public Task InvokeAsync(WebSocketMessageContext context)
 {
     return(Task.CompletedTask);
 }
Exemplo n.º 23
0
 public async Task InvokeAsync(WebSocketMessageContext context)
 {
     var connection = context.GetConnectionId();
     await _connectionManager.BroadcastAsync(context);
 }
Exemplo n.º 24
0
        public async Task InvokeAsync(WebSocketMessageContext context)
        {
            var values = await Task.Run(() => JsonConvert.SerializeObject(context));

            Console.WriteLine(values);
        }