Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR demo not working

I'm following this quick tutorial on getting a SignalR project started, and in my Hub class, I have the following

using Microsoft.AspNet.SignalR.Hubs;

namespace MvcApplication8.Hubs
{
    public class ChatHub : Hub
    {
        public void BroadcastMessage(string message)
        {
            Clients.writeMessage(message);
        }
    }
}

Client code:

    $(document).ready(function () {
        var chat = $.connection.chatHub;
        chat.writeMessage = function(msg) {
            $("#messages").append("<li>" + msg + "</li>");
        };
        $("#buttonSubmit").click(function () {
            chat.broadcastMessage($("#txtInput").val());
        });
        $.connection.hub.start();
    }); 

However I'm getting a compile time error saying:

'Microsoft.AspNet.SignalR.Hubs.HubConnectionContext' does not contain a definition for 'writeMessage' and no extension method 'writeMessage' accepting a first argument of type 'Microsoft.AspNet.SignalR.Hubs.HubConnectionContext' could be found (are you missing a using directive or an assembly reference?)

Where went wrong?

I installed SignalR into my project from http://nuget.org/packages/microsoft.aspnet.signalr using the package manager console "Install-Package Microsoft.AspNet.SignalR -Pre"

like image 759
Null Reference Avatar asked Nov 28 '12 14:11

Null Reference


People also ask

Does SignalR require sticky session?

SignalR requires that all HTTP requests for a specific connection be handled by the same server process. When SignalR is running on a server farm (multiple servers), "sticky sessions" must be used. "Sticky sessions" are also called session affinity by some load balancers.

How long do SignalR connections stay open?

The default keepalive timeout period is currently 20 seconds. If your client code tries to call a Hub method while SignalR is in reconnecting mode, SignalR will try to send the command.


1 Answers

Did you create the corresponding client-function?

<script type="text/javascript">
    $(function () {
        var chat = $.connection.chat;
        chat.writeMessage = function (message) {
            $('#messages').append('<li>' + message + '</li>');
        };
        $.connection.hub.start();
    });
</script>
like image 95
Alexander Zbinden Avatar answered Sep 20 '22 15:09

Alexander Zbinden