Пример #1
0
 public IClientInvoker Group(string group)
 {
     return(new ClientInvoker(
                async(methodName, args) => {
         var message = new Message()
         {
             MessageType = MessageType.ClientMethodInvocation,
             Data = MessageSerializer.SerializeObject(new InvocationDescriptor()
             {
                 MethodName = methodName,
                 Arguments = args
             })
         };
         await _backplane.SendMessageGroupAsync(group, message).ConfigureAwait(false);
     },
                async msg => {
         await _backplane.SendMessageAsync(group, msg).ConfigureAwait(false);
     },
                (g) => {
         throw new NotImplementedException();
     },
                (g) => {
         throw new NotImplementedException();
     }));
 }
Пример #2
0
        public ClientsDispatcher(IWebSocketConnectionManager manager, IBackplane backplane, ILogger <ClientsDispatcher> logger)
        {
            Manager   = manager;
            Backplane = backplane;
            _logger   = logger;

            All = new ClientInvoker(
                async(methodName, args) =>
            {
                var message = new Message()
                {
                    MessageType = MessageType.ClientMethodInvocation,
                    Data        = MessageSerializer.SerializeObject(new InvocationDescriptor()
                    {
                        MethodName = methodName,
                        Arguments  = args
                    })
                };
                await Backplane.SendMessageAllAsync(message).ConfigureAwait(false);
            },
                async msg =>
            {
                await Backplane.SendMessageAllAsync(msg).ConfigureAwait(false);
            },
                async group => {
                await Backplane.SubscribeAll(group).ConfigureAwait(false);
            },
                async group => {
                await Backplane.UnsubscribeAll(group).ConfigureAwait(false);
            });
        }
Пример #3
0
 private async Task SendMessageToSocketAsync(WebSocketHandler handler, Connection connection, string methodName, params object[] args)
 {
     var serializedMessage = MessageSerializer.SerializeObject(new InvocationDescriptor()
     {
         Id         = Interlocked.Increment(ref _nextId).ToString(),
         MethodName = methodName,
         Arguments  = args
     });
     await handler.ReceiveAsync(connection, serializedMessage);
 }
Пример #4
0
 public static Task InvokeGroupAsync(this IBackplane backplane, string groupName, string methodName, params object[] arguments)
 {
     return(backplane.SendMessageGroupAsync(groupName, new Message
     {
         MessageType = MessageType.ClientMethodInvocation,
         Data = MessageSerializer.SerializeObject(new InvocationDescriptor()
         {
             MethodName = methodName,
             Arguments = arguments
         })
     }));
 }
Пример #5
0
        public static async Task InvokeClientMethodAsync(this IChannel channel, string methodName, object[] args)
        {
            // TODO: Serializer settings?
            var message = new Message()
            {
                MessageType = MessageType.ClientMethodInvocation,
                Data        = MessageSerializer.SerializeObject(new InvocationDescriptor()
                {
                    MethodName = methodName,
                    Arguments  = args
                })
            };

            await channel.SendMessageAsync(message).ConfigureAwait(false);
        }
Пример #6
0
        public async void InvokeMethodInvokesBackplaneSendMessage(string connectionId, string methodName, params object[] arguments)
        {
            var backplane       = new TestBackplane();
            var serviceProvider = CreateServiceProvider(o => {
                o.AddSingleton <IBackplane>(_ => backplane);
            });
            var clientDispatcher = ActivatorUtilities.CreateInstance <ClientsDispatcher>(serviceProvider);

            var exception = await Assert.ThrowsAnyAsync <NotImplementedException>(
                () => clientDispatcher.Client(connectionId).InvokeAsync(methodName, arguments));

            Assert.Equal(connectionId + MessageSerializer.SerializeObject <InvocationDescriptor>(new InvocationDescriptor {
                MethodName = methodName,
                Arguments  = arguments
            }), exception.Message);
        }
Пример #7
0
        public static async Task SendMessageAsync(this IChannel channel, Message message)
        {
            // Don't send message to a channel that isn't open.
            if (channel.State != ChannelState.Open)
            {
                return;
            }

            // TODO: Serializer settings? Usage is inconsistent in the entire solution.
            var serializedMessage = MessageSerializer.SerializeObject(message);
            var bytes             = Encoding.ASCII.GetBytes(serializedMessage);

            using (var stream = new MemoryStream(bytes))
            {
                await channel.SendAsync(stream);
            }
        }
Пример #8
0
        public IClientInvoker Client(string connectionId)
        {
            var connection = Manager.GetConnectionById(connectionId);

            return(new ClientInvoker(
                       async(methodName, args) => {
                if (connection != null)
                {
                    await connection.Channel.InvokeClientMethodAsync(methodName, args).ConfigureAwait(false);
                }
                else
                {
                    var message = new Message()
                    {
                        MessageType = MessageType.ClientMethodInvocation,
                        Data = MessageSerializer.SerializeObject(new InvocationDescriptor()
                        {
                            MethodName = methodName,
                            Arguments = args
                        })
                    };
                    await Backplane.SendMessageAsync(connectionId, message).ConfigureAwait(false);
                }
            },
                       async msg => {
                if (connection != null)
                {
                    await connection.Channel.SendMessageAsync(msg).ConfigureAwait(false);
                }
                else
                {
                    await Backplane.SendMessageAsync(connectionId, msg).ConfigureAwait(false);
                }
            },
                       async group => {
                await Backplane.Subscribe(group, connectionId).ConfigureAwait(false);
            },
                       async group => {
                await Backplane.Unsubscribe(group, connectionId).ConfigureAwait(false);
            }));
        }