2013-04-13 4 views
0

J'ai une version 5.3.0 de l'auto-hébergement signalr qui est en cours de mise à niveau vers une version plus récente de signalr. Utilisation de l'exemple https://github.com/SignalR/SignalR/wiki/Self-host J'ai créé un exemple simple, mais je n'arrive pas à le faire fonctionner.Signalr Owin exemple simple client javascript non appelé

Je peux obtenir une connexion au concentrateur sur le serveur et appeler des méthodes sur le concentrateur, mais je n'arrive pas à obtenir le concentrateur pour appeler le client javascript. Quand je regarde cela dans fiddler, je ne vois jamais de réponse du hub.

Voici le code

using System; 
using Microsoft.AspNet.SignalR; 
using Microsoft.Owin.Hosting; 
using Owin; 



namespace ConsoleApplication3 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     string url = "http://localhost:8080/"; 

     using (WebApplication.Start<Startup>(url)) 
     { 
      Console.WriteLine("Server running on {0}", url); 
      Console.ReadLine(); 
     } 
    } 
} 
} 

using Microsoft.AspNet.SignalR; 
using Owin; 

namespace ConsoleApplication3 
{ 
    class Startup 
    { 
    // This method name is important 
    public void Configuration(IAppBuilder app) 
    { 
     var config = new HubConfiguration 
     { 
      EnableCrossDomain = true, 
      EnableJavaScriptProxies = true 
     }; 

     app.MapHubs(config); 
    } 

    } 

}

using System; 
using System.Threading.Tasks; 
using Microsoft.AspNet.SignalR; 
using Newtonsoft.Json; 

namespace ConsoleApplication3.Hubs 
{ 
public class Chat : Hub 
{ 

    public override Task OnConnected() 
    { 
     Notify(Context.ConnectionId); 
     return new Task(() => { }); 
    } 

    public void RunTest() 
    { 
     Notify(Context.ConnectionId); 
    } 


    public void Notify(string connectionId) 
    { 
     dynamic testMessage = new 
     { 
      Count = 3, 
      Message = "Some test message", 
      Timestamp = DateTime.Now 
     }; 

     String json = JsonConvert.SerializeObject(testMessage); 

     var context = GlobalHost.ConnectionManager.GetHubContext<Chat>(); 
     context.Clients.Client(connectionId).sendNotification(json); 
    } 

} 
} 

Et voici le côté client

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title>  
    <script src="Scripts/json2.js"></script> 
    <script src="Scripts/jquery-1.9.1.js"></script> 
    <script src="Scripts/jquery.signalR-1.0.1.js"></script> 
    <script src="http://localhost:8080/signalr/hubs"></script> 

<script>   
    $(function() { 

     // Proxy created on the fly 
     var notification = $.connection.chat; 
     $.connection.hub.logging = true; 

     // Declare functions that can be run on the client by the server 
     notification.client.sendNotification = onAddNotification; 
     notification.client.disconnected = function (connectionid) { 
      console.log(connectionid); 
     }; 
     // Testing code only 
     $("#testButton").click(function() { 
      // Run test function on server 
      notification.server.runTest(); 
     }); 

     jQuery.support.cors = true; 

     // Map the onConnect and onDisconnect functions 
     notification.client.connected = function() { 
      alert("Notification system connected"); 
     }; 
     notification.client.disconnected = function() { }; 
     $.connection.hub.url = "http://localhost:8080/signalr"; 

     //$.connection.hub.start(); 
     $.connection.hub.start(function() { 
      alert("Notification system connected"); 
     }); 

    }); 

    // Process a newly received notification from the server 
    function onAddNotification(message) { 

     // Convert the passed json message back into an object 
     var obj = JSON.parse(message); 

     var parsedDate = new Date(parseInt(obj.Timestamp.substr(6))); 

     // Update the notification list 
     $('#notifications').prepend('<li>' + obj.Message + ' at ' + parsedDate + '</li>'); 

    }; 

    </script> 
</head> 
<body>  
    <a href="javascript:void(0)" class="btn" id="testButton">Send test</a>  
    <ul class="unstyled" id="notifications">    
    </ul> 
</body> 

Toutes les idées serait apprécié, puisque je suis assez coincé.

Répondre

2

Peu de choses dans votre code:

Changer ceci:

public override Task OnConnected() 
{ 
    Notify(Context.ConnectionId); 
    return new Task(() => { }); 
} 

À:

public override Task OnConnected() 
{ 
    Notify(Context.ConnectionId); 
    return base.OnConnected(); 
} 

également dans votre centre:

Cette fonction est trop recherché:

public void Notify(string connectionId) 
{ 
    dynamic testMessage = new 
    { 
     Count = 3, 
     Message = "Some test message", 
     Timestamp = DateTime.Now 
    }; 

    String json = JsonConvert.SerializeObject(testMessage); 

    var context = GlobalHost.ConnectionManager.GetHubContext<Chat>(); 
    context.Clients.Client(connectionId).sendNotification(json); 
} 

Je ne suis même pas sûr de savoir pourquoi vous passez l'identifiant de connexion (peut-être il était censé être statique?)

public void Notify() 
{ 
    dynamic testMessage = new 
    { 
     Count = 3, 
     Message = "Some test message", 
     Timestamp = DateTime.Now 
    }; 

    Clients.Client(Context.ConnectionId).sendNotification(testMessage); 
} 

Vous ne nous faisons déjà pas besoin de sérialisation deux fois pour vous .

Supprimer:

jQuery.support.cors = true; 

Ne réglez jamais cela.

aussi:

// Map the onConnect and onDisconnect functions 
notification.client.connected = function() { 
    alert("Notification system connected"); 
}; 

notification.client.disconnected = function() { }; 

Ce ne sont pas CARTOGRAPHIE côté client quoi que ce soit. Vous ne pouvez pas mapper connecté et déconnecté du serveur au client. Le client a ses propres événements.

Autres choses:

Cela devrait être à l'intérieur de la fonction de rappel de démarrage afin que vous ne frappez pas avant qu'il ne soit prêt:

$.connection.hub.start().done(function() { 
    // Testing code only 
    $("#testButton").click(function() { 
     // Run test function on server 
     notification.server.runTest(); 
    }); 
}); 
+0

Merci David qui l'a fait. Le problème que j'ai maintenant est que je me connecte à deux hubs sur des URL différentes et quand je fais ceci: - 'code' var cfgBrokerHubConnection = $ .hubConnection (cfgBrokerSignalRHubServer); var cfgBroker = cfgBrokerHubConnection.createHubProxy ('cfgBrokerHub'); Je reçois une erreur sur cette ligne cfgBroker.client.receiveMessage = processCfgMessage (m); dire que le client est indéfini. –

+0

https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs-%28No-Proxy%29 – davidfowl

+0

Je suis confronté au même problème OnConnected, mais je n'utilise pas EnableJavaScriptProxies = true et mon code est [ici ] (https://stackoverflow.com/questions/47138223/angular-signalr-error-during-negotiation-request) mais a résolu ce problème. Maintenant obtenir l'ID de connexion mais OnConnected pas tiré :( – Developer

Questions connexes