internal void RegisterHandle(string methodName, HubMethodCallback callback)
        {
            if (this.callbacks.TryGetValue(methodName, out var methodHandlers))
            {
                methodHandlers[callback.Id] = callback;
            }
            else
            {
                this.callbacks[methodName] = new Dictionary <string, HubMethodCallback>
                {
                    { callback.Id, callback }
                };
            }

            this.runtime.InvokeSync <object>(ON_METHOD, this.InternalConnectionId, DotNetObjectReference.Create(callback));
        }
Пример #2
0
        public IDisposable On <TResult>(string methodName, Func <TResult, Task> handler)
        {
            if (string.IsNullOrEmpty(methodName))
            {
                throw new ArgumentNullException(nameof(methodName));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            var callbackId = Guid.NewGuid().ToString();

            var callback = new HubMethodCallback(callbackId, methodName, this,
                                                 (json) =>
            {
                var payload = Json.Deserialize <TResult>(json);
                return(handler(payload));
            }
                                                 );

            if (this._callbacks.TryGetValue(methodName, out var methodHandlers))
            {
                methodHandlers[callback.Id] = callback;
            }
            else
            {
                this._callbacks[methodName] = new Dictionary <string, HubMethodCallback>
                {
                    { callback.Id, callback }
                };
            }

            ((IJSInProcessRuntime)JSRuntime.Current).Invoke <object>(ON_METHOD, this.InternalConnectionId, new DotNetObjectRef(callback));

            //HubConnectionManager.On(this.InternalConnectionId, callback);
            return(callback);
        }