MVC SignalR服务器和Windows服务SIgnalR客户端之间的SignalR

use*_*435 15 asp.net-mvc windows-services signalr

我已经知道在这里问我可以在一个普通的旧Windows服务中使用SignalR.我想在Windows服务之间作为SignalR客户端和ASP.NET MVC SignalR服务器进行通信.它说,我使用了这个集线器指南

指定URL的服务器代码

app.MapSignalR("/signalr", new HubConfiguration());
Run Code Online (Sandbox Code Playgroud)

如果我这样做,消息会在哪里发生?进行新的HubConfiguration时,什么是集线器?

它说我可以联系

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

我会在我的Windows Server中使用它,但如果我将文本发送到服务器上的http://myserver.com/signalr,我该如何获取它?什么集线器?

另外最好把它放在Windows服务中?一个例子会很棒!

use*_*435 16

这解决了这个问题

在客户端(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);
            }
        }
Run Code Online (Sandbox Code Playgroud)

在服务器上:

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

        }
Run Code Online (Sandbox Code Playgroud)

  • @VAAA,您所需要的只是Windows服务框上的Microsoft.AspNet.SignalR.Client。好答案! (2认同)