SignalR JS Client Methods Not Invoked

蓝咒 提交于 2019-11-29 13:54:29
Zac Seth

So, the answer was actually quite simple (though since the last release I have upgraded from 1.0.0.0-rc1 to 1.0.0.0 - but I'm fairly certain that didn't change this scenario signficantly).

I found the answer here: https://stackoverflow.com/a/15074002/32935 (note, specifically, my solution was for the first answer provided - though the second helped me identify the cause).

Essentially, for those who don't want to go over to the original answer, I had to setup my client methods before calling the start() method against the connection as opposed to in the done() callback as I was doing. Here's an example:

$.connection.message.client.addMessage = function (message) {
    alert( 'Now it works!' );
};

$.connection.hub.start()
    .done(function () {
        console.log( 'Connection established!' );
    });

When you hover over the Clients object, do you see all the functions you defined in JS?

Anyway,I'm not sure why your way doesn't work, but this is how I wrote my client side, and it works. Maybe you could try this way.

$(function () {

    var hub = $.connection.RatesHub;


    $.connection.hub.start().done(function () {
        /*Logic goes here*/
    });

    $.extend(hub.client, {
        FuncName: function (msg) {
            /*Logic goes here*/
        }
    });

}

Any function you want to be recognized in your server, use $.extend.

Hope that helps.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!