signalr - TypeError: chat is undefined

4

I am developing a simple chat with signalr and I have run into an error that I still can not solve.

It says that the variable chat is not defined which is not true, since I am declaring it.

var chat = $.connection.chat;

$(function () {
    chat.client.nuevoMensaje = onNewMessage;
    $.connection.hub.start();
});

function onNewMessage(message) {
    chat.server.enviarMensaje("hola");
};

I have added all the required references.

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

This is the Hub that I created on the server side.

[HubName("Chat")]
public class ChatHub : Hub
{
    public void EnviarMensaje(string mensaje)
    {
        var msg = string.Format("{0} {1}", Context.ConnectionId, mensaje);
        Clients.All.nuevoMensaje(msg);
    }

    public void UnirSala(string sala)
    {
        Groups.Add(Context.ConnectionId, sala);
    }

    public void EnviarMensajeSala(string sala, string mensaje)
    {
        var msg = string.Format("{0} {1}", Context.ConnectionId, mensaje);
        Clients.Group(sala).nuevoMensaje(msg);
    }

    public void EnviarMensajeData(EnviarData data)
    {
        Clients.All.nuevaData(data);
    }


    public override Task OnConnected()
    {
        EnviarDataAMonitor("Conectado", Context.ConnectionId);
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        EnviarDataAMonitor("Desconectado", Context.ConnectionId);
        return base.OnDisconnected(stopCalled);
    }

    public override Task OnReconnected()
    {
        EnviarDataAMonitor("Reconexción", Context.ConnectionId);
        return base.OnReconnected();
    }

    private void EnviarDataAMonitor(string typoEvento, string connectionId)
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<MonitorHub>();
        context.Clients.All.nuevoEvento(typoEvento, connectionId);
    }
}

Does anyone know the reason for the error, knowing that I have already declared the variable?

    
asked by Andres Felipe Williams Suarez 06.12.2015 в 18:19
source

3 answers

6

I already found the problem!

When I was editing my question, I was testing a possible solution.

The name of hub was having a bad time. On the server side, the automatic script that was generated had the name Chat but on the client it was calling chat .

Hub

[HubName("Chat")]
public class ChatHub : Hub
{
    .......

Client Code

var chat = $.connection.chat;

In this case there are two solutions, or change the name of the Hub, or change the call in the client code.

Changing the Hub

[HubName("chat")]
public class ChatHub : Hub
{
    .....

Changing the client code

var chat = $.connection.Chat;

Either of these two cases are valid, the important thing is that the name of hub is called the same on the client side, if the name of hub is mensaje the call in javascript should be $.connection.mensaje;

    
answered by 06.12.2015 / 19:28
source
1

The only explanation I can come up with is that $.connection.chat returns undefined and is assigned to your variable chat , so in the next line it gives an error

    
answered by 06.12.2015 в 18:25
0

The chat variable is outside scope in the onNewMessage method and that's why it tells you it is not defined.

I would move the onNewMessage function within the document.ready () of jQuery, that is:

$(function () {
    var chat = $.connection.chat;
    chat.client.nuevoMensaje = onNewMessage;
    $.connection.hub.start();

    function onNewMessage(message) {
      chat.server.enviarMensaje("hola");
   };

});
    
answered by 06.12.2015 в 19:01