加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在MVC SignalR服务器和Windows服务SIgnalR客户端

发布时间:2020-12-15 18:58:20 所属栏目:asp.Net 来源:网络整理
导读:我已经知道在这里,我可以在一个普通的 Windows服务中使用SignalR.我想在Windows Server作为SignalR Client和ASP.NET MVC SignalR Server之间进行通信.我用 this hubs guide,说 指定URL的服务器代码 app.MapSignalR("/signalr",new HubConfiguration()); 如果
我已经知道在这里,我可以在一个普通的 Windows服务中使用SignalR.我想在Windows Server作为SignalR Client和ASP.NET MVC SignalR Server之间进行通信.我用 this hubs guide,说

指定URL的服务器代码

app.MapSignalR("/signalr",new HubConfiguration());

如果我这样做,邮件将在哪里?当您做新的HubConfiguration时,集线器是什么?

它说我可以连接

NET client code that specifies the URL
 var hubConnection = new HubConnection("http://contoso.com/signalr",useDefaultUrl: false);

我会在我的Windows Server中,但是如果我在服务器上发送文本到http://myserver.com/signalr,我该如何得到它?什么中心?

还有哪里最好把它放在Windows服务中?一个很棒的例子

解决方法

这解决了这个问题

在客户端(Windows服务)上:

protected override async void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart");
            try
            {
                var hubConnection = new HubConnection("http://www.savitassa.com/signalr",useDefaultUrl: false);
                IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");

                await hubConnection.Start();

                await alphaProxy.Invoke("Hello","Message from Service");
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry(ex.Message);
            }
        }

在服务器上:

public class AlphaHub : Hub
    {
        public void Hello(string message)
        {
            // We got the string from the Windows Service 
            // using SignalR. Now need to send to the clients
            Clients.All.addNewMessageToPage(message);

            // Send message to Windows Service

        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读