2017-08-29 5 views
0

Ich versuche, eine Coss-Domain-Verbindung zu etablieren. In meinem JavaScript-Client sehe ich kein Problem mit der Verbindung, aber wenn ich ein Signal vom Hub sende, wird die Javascript-Methode nicht getroffen. In der Konsole wird kein Fehler angezeigt. Die JavaScript-Variable "Chat" ist definiert. Ich habe mir mehrere Tutorials im Internet angeschaut und den Code größtenteils kopiert, aber es funktioniert nicht.SignalR Cross-Domain-Konsole Anwendung Host mit JavaScript-Client Problem

Console Application host:

class Program 
    { 
     static void Main(string[] args) 
     { 
      string url = "http://localhost:43253/"; 
      using (WebApp.Start(url)) 
      { 
       Console.WriteLine($"Server running on {url}"); 
       Console.ReadKey(); 
       MyHub hub = new MyHub(); 
       hub.Send("test", "test"); 
       Console.ReadKey(); 
      } 
     } 
    } 

    class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.Map("/signalr", map => 
      { 
       // Setup the cors middleware to run before SignalR. 
       // By default this will allow all origins. You can 
       // configure the set of origins and/or http verbs by 
       // providing a cors options with a different policy. 
       map.UseCors(CorsOptions.AllowAll); 

       var hubConfiguration = new HubConfiguration 
       { 

       }; 

       // Run the SignalR pipeline. We're not using MapSignalR 
       // since this branch is already runs under the "/signalr" 
       // path. 
       map.RunSignalR(hubConfiguration); 
      }); 
     } 
    } 

    public class MyHub : Hub 
    { 
     public override Task OnConnected() 
     { 
      Console.WriteLine("connected"); 
      return base.OnConnected(); 
     } 

     public void Send(string name, string message) 
     { 
      var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); 
      context.Clients.All.addMessage(name, message); 
     } 
    } 

JavaScript-Client:

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index</title> 

</head> 
<body> 
    <div id="image"> 
    </div> 
    <script src="~/Scripts/jquery-1.6.4.min.js"></script> 
    <script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script> 
    <script src="http://localhost:43253/signalr/hubs"></script> 
    <script type="text/javascript"> 
     $(function() { 
      $.connection.hub.url = "http://localhost:43253/signalr"; 
      var chat = $.connection.myHub; 

      chat.client.addMessage = function (name, message) { 
       console.log(name + message); 
      } 
     }); 
    </script> 
</body> 
</html> 

Antwort

0

ich es fest. Das Problem war, dass ich vergessen habe, die Verbindung zu starten. Gerade hinzugefügt: $ .connection.hub.start(); am Ende des JavaScript-Clients