Exemplo n.º 1
0
 public async Task <object> ReceiveStringAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
 {
     if (_recieveMessageHandler != null)
     {
         Func <object[], object> Handler = (args) => { _recieveMessageHandler(args); return(null); };
         return(await Task.Run(() => Handler(invocationDescriptor.Arguments)));
     }
     else
     {
         return(await Task.FromResult <object>(null));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Called when an invoke method call has been received.
        /// </summary>
        /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
        /// <param name="invocationDescriptor">
        /// The invocation descriptor containing the method name and parameters.
        /// </param>
        /// <returns>Awaitable Task.</returns>
        public override async Task <object> OnInvokeMethodReceivedAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
        {
            if (!_handlers.ContainsKey(invocationDescriptor.MethodName))
            {
                //throw new Exception($"Received unknown command '{invocationDescriptor.MethodName}'.");
                return(await Task.Run(() => _defaultHandler.Handler(invocationDescriptor.Arguments)));
            }
            var invocationHandler = _handlers[invocationDescriptor.MethodName];

            if (invocationHandler != null)
            {
                return(await Task.Run(() => invocationHandler.Handler(invocationDescriptor.Arguments)));
            }
            return(await Task.FromResult <object>(null));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Called when an invoke method call has been received.
        /// </summary>
        /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
        /// <param name="invocationDescriptor">
        /// The invocation descriptor containing the method name and parameters.
        /// </param>
        /// <returns>Awaitable Task.</returns>
        public override async Task <object> OnInvokeMethodReceivedAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
        {
            // there must be a separator in the method name.
            if (!invocationDescriptor.MethodName.Contains(Separator))
            {
                throw new Exception($"Invalid controller or method name '{invocationDescriptor.MethodName}'.");
            }

            // find the controller and the method name.
            string[] names      = invocationDescriptor.MethodName.Split(Separator);
            string   controller = names[0].ToLower();
            string   command    = Prefix + names[1];

            // find the desired controller.
            if (Controllers.TryGetValue(controller, out object self))
            {
                // use reflection to find the method in the desired controller.
                MethodInfo method = self.GetType().GetMethod(command);

                // if the method could not be found:
                if (method == null)
                {
                    throw new Exception($"Received unknown command '{command}' for controller '{controller}'.");
                }

                // optionally insert client as parameter.
                List <object> args = invocationDescriptor.Arguments.ToList();
                if (!NoWebsocketArgument)
                {
                    args.Insert(0, socket);
                }

                // call the method asynchronously.
                try
                {
                    return(await Task.Run(() => method.Invoke(self, args.ToArray())));
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
            }
            else
            {
                throw new Exception($"Received command '{command}' for unknown controller '{controller}'.");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Called when an invoke method call has been received.
        /// </summary>
        /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
        /// <param name="invocationDescriptor">
        /// The invocation descriptor containing the method name and parameters.
        /// </param>
        /// <returns>Awaitable Task.</returns>
        public override async Task <object> OnInvokeMethodReceivedAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
        {
            if (invocationDescriptor.MethodName == "ReceiveString")
            {
                if (_recieveMessageHandler != null)
                {
                    Func <object[], object> Handler = (args) => { _recieveMessageHandler(args); return(null); };
                    return(await Task.Run(() => Handler(invocationDescriptor.Arguments)));
                }
                else
                {
                    return(await Task.FromResult <object>(null));
                }
            }
            if (!_handlers.ContainsKey(invocationDescriptor.MethodName))
            {
                throw new Exception($"Received unknown command '{invocationDescriptor.MethodName}'.");
            }
            var invocationHandler = _handlers[invocationDescriptor.MethodName];

            if (invocationHandler != null)
            {
                return(await Task.Run(() => {
                    foreach (var handle in invocationHandler)
                    {
                        invocationHandler[0].Handler(invocationDescriptor.Arguments);
                    }

                    return new object();
                }));
            }
            return(await Task.FromResult <object>(null));
        }
        /// <summary>
        /// Called when an invoke method call has been received.
        /// </summary>
        /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
        /// <param name="invocationDescriptor">
        /// The invocation descriptor containing the method name and parameters.
        /// </param>
        /// <returns>Awaitable Task.</returns>
        public override async Task <object> OnInvokeMethodReceivedAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
        {
            // create the method name that has to be found.
            string command = Prefix + invocationDescriptor.MethodName;

            // use reflection to find the method in the desired controller.
            MethodInfo method = Controller.GetType().GetMethod(command);

            // if the method could not be found:
            if (method == null)
            {
                throw new Exception($"Received unknown command '{command}' for controller '{Controller.GetType().Name}'.");
            }

            // optionally insert client as parameter.
            List <object> args = invocationDescriptor.Arguments.ToList();

            if (!NoWebsocketArgument)
            {
                args.Insert(0, socket);
            }

            // call the method asynchronously.
            try
            {
                return(await Task.Run(() => method.Invoke(Controller, args.ToArray())));
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }
        /// <summary>
        /// Called when an invoke method call has been received.
        /// </summary>
        /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
        /// <param name="invocationDescriptor">
        /// The invocation descriptor containing the method name and parameters.
        /// </param>
        /// <returns>Awaitable Task.</returns>
        public override async Task <object> OnInvokeMethodReceivedAsync(string socketId, InvocationDescriptor invocationDescriptor)
        {
            if (!_handlers.ContainsKey(invocationDescriptor.MethodName))
            {
                throw new Exception($"Received unknown command '{invocationDescriptor.MethodName}'.");
            }
            var invocationHandler = _handlers[invocationDescriptor.MethodName];

            if (invocationHandler != null)
            {
                List <object> args = new List <object>();
                if (invocationDescriptor.Params is JArray)
                {
                    JArray array = (JArray)invocationDescriptor.Params;
                    args = array.ToObject <List <object> >();
                }
                else if (invocationDescriptor.Params is JObject)
                {
                    args.Add(invocationDescriptor.Params as object);
                }
                //if (!NoWebsocketArgument)
                //    args.Insert(0, socketId);
                return(await Task.Run(() => invocationHandler.Handler(args.ToArray())));
            }
            return(await Task.FromResult <object>(null));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Called when an invoke method call has been received.
        /// </summary>
        /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
        /// <param name="invocationDescriptor">
        /// The invocation descriptor containing the method name and parameters.
        /// </param>
        /// <returns>Awaitable Task.</returns>

        public override async Task <object> OnInvokeMethodReceivedAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
        {
            return(await ReceiveStringAsync(socket, invocationDescriptor));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Called when an invoke method call has been received.
        /// </summary>
        /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
        /// <param name="invocationDescriptor">
        /// The invocation descriptor containing the method name and parameters.
        /// </param>
        /// <returns>Awaitable Task.</returns>
        public override async Task <object> OnInvokeMethodReceivedAsync(string socketId, InvocationDescriptor invocationDescriptor)
        {
            // create the method name that has to be found.
            string command = Prefix + invocationDescriptor.MethodName;

            // use reflection to find the method in the desired controller.
            MethodInfo method = Controller.GetType().GetMethod(command);

            // if the method could not be found:
            if (method == null)
            {
                throw new Exception($"Received unknown command '{command}' for controller '{Controller.GetType().Name}'.");
            }
            List <object> args = new List <object>();

            if (invocationDescriptor.Params is JArray)
            {
                JArray array = (JArray)invocationDescriptor.Params;
                args = array.ToObject <List <object> >();
            }
            else if (invocationDescriptor.Params is JObject)
            {
                args.Add(invocationDescriptor.Params as object);
            }
            else
            {
                args.Add(invocationDescriptor.Params as object);
            }
            if (!NoWebsocketArgument)
            {
                args.Insert(0, socketId);
            }
            // call the method asynchronously.
            try
            {
                return(await Task.Run(() => method.Invoke(Controller, args.ToArray())));
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Called when an invoke method call has been received.
        /// </summary>
        /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
        /// <param name="invocationDescriptor">
        /// The invocation descriptor containing the method name and parameters.
        /// </param>
        /// <returns>Awaitable Task.</returns>
        public override async Task <object> OnInvokeMethodReceivedAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
        {
            if (_recieveMessageHandler != null)
            {
                Func <object[], object> Handler = (args) => { _recieveMessageHandler(args); return(null); };
                _ = Task.Run(() => Handler(invocationDescriptor.Arguments));
            }


            var channel = invocationDescriptor.Channel;

            if (!string.IsNullOrEmpty(channel))
            {
                //var publishHandlersForChannel = _handlers[channel];

                var publishHandlersForChannel = from result in _handlers
                                                where Regex.Match(channel, result.Key, RegexOptions.Singleline | RegexOptions.IgnoreCase).Success
                                                select result.Value;

                await Task.Run(() =>
                {
                    foreach (var handlers in publishHandlersForChannel)
                    {
                        foreach (var handle in handlers)
                        {
                            handle.Handler(invocationDescriptor.Arguments);
                        }
                    }

                    return(new object());
                });
            }
            if (!_handlers.ContainsKey(invocationDescriptor.MethodName))
            {
                throw new Exception($"Received unknown command '{invocationDescriptor.MethodName}'.");
            }
            var invocationHandler = _handlers[invocationDescriptor.MethodName];

            if (invocationHandler != null)
            {
                return(await Task.Run(() => {
                    foreach (var handle in invocationHandler)
                    {
                        invocationHandler[0].Handler(invocationDescriptor.Arguments);
                    }

                    return new object();
                }));
            }
            return(await Task.FromResult <object>(null));
        }
 /// <summary>
 /// Called when an invoke method call has been received.
 /// </summary>
 /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
 /// <param name="invocationDescriptor">
 /// The invocation descriptor containing the method name and parameters.
 /// </param>
 /// <returns>Awaitable Task.</returns>
 public virtual Task <object> OnInvokeMethodReceivedAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
 {
     throw new NotImplementedException();
 }