【问题标题】:Signalr N-Layers / Class library get updates from Server to ClientsSignalr N-Layers / 类库从服务器到客户端获取更新
【发布时间】:2014-11-19 17:36:07
【问题描述】:

我是这个 (Signalr) 的新手,我一直在阅读这个,但是我没有找到,而且我不知道我是否能以更好的形式做这些事情。

我想要做的是从我的类库通知向客户端发送更新,当从 Windows 服务向数据库发送插入时,我执行插入并调用您将通知发送到的集线器(类库通知)客户和图形的实时更新

我制作了类库通知,从 nuget signalr 客户端和组件安装,并构建了所有但这不起作用,这是代码:

这是 Project Windows 服务中的方法:

public void ProcessResults()
        {
            RuleDto ruleToExecute;
            using (var svc = new ServiceWrapper<ICentralMonitor>(ConfigurationManager.AppSettings["MonitorBinding"]))
            {
                 ruleToExecute = svc.Channel.GetRulesToApplyByValidationGroup(Id, _results.ToDictionary(m => m.Key.Id, pair => pair.Value));
            }

            if (ruleToExecute != null)
            {   
                foreach (var action in ruleToExecute.Actions)
                {
                    object[] parameters = { Id };
                    InvokeHandler.InvokeAction(action.Assembly, action.Namespace, action.Class, action.Method, parameters);
                }

                **NotificationHub notifier = new NotificationHub();
                notifier.NotifierInsertExecutionRule(Id);**
            }

            UpdateCurrentState();
        } 

This is the Project class library Notifications: (This is all what have this project)

public class NotificationHub : Hub
    {
        public async void NotifierInsertExecutionRule(long id)
        {
            var stats = await StatisticsController.CreateModelAsync(id);
            hubContext.Clients.All.updateStatistics(stats);
        }
    }

在项目 web(客户端)中,我也在这个项目中从 Nuget signalr 安装。

我只是警惕地看到这项工作,但什么也不做。

<script type="text/javascript">
    var connection = $.hubConnection();
    var hub = connection.createHubProxy("NotificationHub");
    hub.on("updateStatistics", function (statistics) {

        alert("Se Actualizo");

    });

    connection.start();
</script>

还有启动:

using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(ADC.Monitor.Monitor.Web.App_Start.Startup))]
namespace ADC.Monitor.Monitor.Web.App_Start
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

提前致谢

【问题讨论】:

  • 首先要做的是重载集线器中的OnConnectedonDisconnectedOnReconnecting 方法,并在它们上放置断点以查看是否正在连接

标签: c# model-view-controller signalr layer


【解决方案1】:

该过程的结果是创建一个 HubContext,它不是您的客户端连接的那个。 还可以尝试从客户端调用集线器,至少看看它是否正常工作,你的客户端所做的只是监听。即:

hub.invoke("notifierInsertExecutionRule", someId);

还要从集线器外部访问您的集线器上下文,您必须执行类似的操作。

var hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
hubContext.Clients.All.updateStatistics("Message");

【讨论】:

    猜你喜欢
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2012-09-27
    • 1970-01-01
    相关资源
    最近更新 更多