2016-06-22 8 views
0

私はSignalRハブを作成し、それをWebアプリケーション内で動作させました。今、私はそのハブにメッセージを送るために、別のWindowsサービスを取得しようとしています。ハブ接続URLに何を使用するかに応じて、私は401 UnauthorizedまたはSocketExceptionを取得します。 Windowsサービスがハブにメッセージを送信できるようにするには、何が欠けていますか?ウェブアプリでWindowsサービスからWebアプリケーションのSignalR Hubにアクセスする

スタートアップクラス:ウェブアプリで

[assembly: OwinStartup(typeof(MmaWebClient.Startup))] 
namespace MmaWebClient 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.MapSignalR(); 
     } 
    } 
} 

ハブクラス:index.htmlをで

[HubName("scannerHub")] 
public class ScannerHub : Hub 
{ 
    public void Send(string message) 
    { 
     Clients.All.broadcastMessage(message); 
    } 
} 

のスクリプト参照:

<script src="../scripts/jquery-1.9.1.js"></script> 
<script src="../scripts/jquery.signalR-2.2.0.min.js"></script> 
<script src="../signalr/hubs"></script> 

のJavaScript(作品)で私のAngularJSコントローラ:

Windowsサービスで

そして、最後に、:

var connection = new HubConnection("http://localhost/MmaWebClient/signalr"); 
var connection = new HubConnection("http://localhost:8080"); 
var connection = new HubConnection("http://localhost"); 

まず:私は上記のコードでは、新しいハブ接続を作成するとき

static void Main(string[] args) 
    { 
     AutoResetEvent scanReceivedEvent = new AutoResetEvent(false); 

     var connection = new HubConnection("http://localhost/MmaWebClient/signalr"); 
     //Make proxy to hub based on hub name on server 
     var myHub = connection.CreateHubProxy("scannerHub"); 
     //Start connection 

     connection.Start().ContinueWith(task => { 
      if (task.IsFaulted) { 
       Console.WriteLine("There was an error opening the connection:{0}", 
            task.Exception.GetBaseException()); 
      } else { 
       Console.WriteLine("Connected"); 
      } 

     }).Wait(); 


     myHub.On<string>("broadcastMessage", param => { 
      Console.WriteLine("Scan received from server = [{0}]", param); 
      scanReceivedEvent.Set(); 
     }); 

     string input = ""; 

     do 
     { 
      Console.WriteLine("Enter a value to send to hub or Q to quit."); 

      input = Console.ReadLine(); 

      if (input.ToUpperInvariant() != "Q") 
      { 
       myHub.Invoke<string>("Send", input); 
       scanReceivedEvent.WaitOne(1000); 
      } 

     } while (input.ToUpperInvariant() != "Q"); 

     connection.Stop(); 
    } 

、私は成功せずにこれらの3つのURLを試してみましたURL:401権限
セカンドURL:ソケット例外
第三URL:が見つかりませんでした404

+0

http:// localho st/MmaWebClient'? C#SignalRクライアントの場合、私はsignalr/hubsの部分がないURLを使用しています。 – Wojtek

+0

私はそれを行うと401 Unauthorizedを取得します。 –

答えて

2

私はそれが(第2ライン、下の)接続に資格情報を設定することで動作させることができました:

var connection = new HubConnection("http://localhost/MmaWebClient"); 
connection.Credentials = CredentialCache.DefaultNetworkCredentials; 
var myHub = connection.CreateHubProxy("scannerHub"); 
0

決定的な答えはありませんが、ここではSignalRエンドポイントをマッピングする別の方法があります。このURLで間違ったURLを取得するのは難しいです。

ASP.NET Startup.cs

public void Configuration(IAppBuilder app) 
{  
    app.Map("/foo", map => 
    { 
     //Specify according to your needs 
     var hubConfiguration = new HubConfiguration 
     { 
      EnableDetailedErrors = true 
     }; 
     map.RunSignalR(hubConfiguration); 
    }); 

    ConfigureAuth(app); 
} 

クライアント

var hubConnection = new HubConnection("http://localhost/foo", useDefaultUrl: false); 
var proxy = hubConnection.CreateHubProxy("scannerHub"); 
+0

私はそれを試み、404が見つかりませんでした。私はConfigureAuth行をコメントする必要がありました。私には利用できなかったからです。 –

+0

カスタムポートでWebページを実行しましたか? – supertopi

関連する問題