2011-11-28 13 views
5

Ho classe hub:SignalR javascript richiamata non funziona

public class ChatHub : Hub 
{ 
    // Send message 
    public void Send(string message) 
    { 
     Clients.addMessage(DateTime.Now.ToString("HH:mm:ss") + " " + message); 
    } 
} 

e JavaScript:

// Proxy created on the fly 
var chat = $.connection.chatHub; 

// Declare a function on the chat hub so the server can invoke it 
chat.addMessage = function(message) { 
    alert("message:" + message); 
    $('#chat').append('<div>' + message + '</div>'); 
}; 


$("#sendButton").click(function() { 

    // Call the chat method on the server 
    chat.send($('#message').val()) 
     .done(function() { 
      console.log('Success!') 
     }) 
     .fail(function (e) { 
      console.warn(e); 
     }) 
}); 

// Start the connection 
$.connection.hub.start(); 

Tutti conection vanno bene: enter image description here

Se uso punto di interruzione in qui Clienti. addMessage (DateTime.Now.ToString ("HH: mm: ss") + "" + messaggio); tutto va bene.

Ma non ricevo il callback sulla funzione javascript. avviso ("messaggio:" + messaggio); non esegue mai

+0

Il codice sembra buono. Dove lo stai eseguendo? IIS? VS? – davidfowl

+0

@dfowler su ISS 7.5 –

+0

Il codice javascript è riportato sopra nell'evento pronto o simile? – escouser

risposta

3

Hai aggiunto l'hub Signalr lato client?

<script src="/signalr/hubs" type="text/javascript"></script>