Ryan
Ryan

Reputation: 5546

signalR - how to call server method from client - code not working

When I try calling SendNewOrderConfirmation on the server side from the client I get

"undefined" is not a function

on line chat.SendNewOrderConfirmation(data); does anyone know why is this happening? Thank you

on the server side

public class DriverChat : Hub, IDisconnect
{

    public void Start(Driver d)
    {
     ...
     }
    public void SendNewOrderConfirmation(OrderDriverData data)
    {
        LogFile.LogResponseTime(data.orderId, data.driverId);
    }
}

on the client side

 function begin(args) {

        try {
            //alert('begin');

            chat = $.connection.driverChat;

            chat.refresh = function () {
                ready++;
            };
            chat.disconnect = function () {
                alert('Server has disconnected');
            };
            $.connection.hub.start(function () {
                chat.start(args);
                ready++;
               // alert('signalR started');
            });
         } catch (e) {
            alert(e.message);
            return e.message;
        }
    }
    function confirmNewOrder(data) {
        try{
            alert('sending cofirmation');
            chat.SendNewOrderConfirmation(data);
            alert('confirmation sent');
        } catch (e) {
            alert(e);
        }
    };
}

Upvotes: 3

Views: 12609

Answers (2)

Tim B James
Tim B James

Reputation: 20364

Depending on the version of SignalR which you are using, the answer could be different.

To begin with, if you are on a version prior to v1 Alpha, then your code should be;

chat.sendNewOrderConfirmation(data);

Notice the lowercase s.

If you are on version v1 Alpha or greater, then you code should be;

chat.server.sendNewOrderConfirmation(data);

Upvotes: 7

Steve B
Steve B

Reputation: 37660

You have to register the hub's javascript generated file. Add this to your page (or your master page):

<script src="<%: ResolveUrl("~/signalr/hubs") %>"></script>

Upvotes: 0

Related Questions