Pusher Ajax调用不会'似乎不起作用

Pusher Ajax call doesn't seem to be working

本文关键字:不起作用 Ajax 调用 Pusher      更新时间:2023-09-26

第一次尝试使用Pusher。。不确定它是否有效。

当页面加载时,我在js代码中放入的console.log语句激发,显示Pusher对象经过以下状态:Initialized > Connecting > Connected

然而,我已经将一个简单的ajax调用绑定到connected事件,希望只发送然后接收Pusher.connection.socked_id。在ajax调用的success函数中,我只想显示返回的socket_id

同样,我的所有显示都显示在控制台中,使应用程序看起来正在工作,包括success回调中的Successful Ajax Call消息,但我没有收到任何数据。data对象显示为undefined,如果我在HandleEvent服务器端方法中设置了断点,它就永远不会触发。

知道我做错了什么吗?

C#

    [HttpPost]
    public void HandleEvent(string socket_id)
    {
        var pusher = new Pusher(PusherConfig.APP_ID(), PusherConfig.KEY(), PusherConfig.SECRET());
        var result = pusher.Trigger("test_channel", "my_event", new { message = socket_id });
    }

Pusher应用程序

$(document).ready(function () {   
Pusher.log = function(message) {
    if (window.console && window.console.log) {
       console.log(message);
    }
};
var pusher = new Pusher(key);
var socketId = null;
var channel = pusher.subscribe('test_channel');
channel.bind('my_event', function (data) {
    alert(data.message);
});
pusher.connection.bind('initialized', function (data) {
    console.log('initialized');
});
pusher.connection.bind('connecting', function (data) {
    console.log('connecting');
});
pusher.connection.bind('connected', function (data) {
    console.log('connected');
    socketId = pusher.connection.socket_id;
    $.ajax({
        url: 'api/Values/HandleEvent',
        type: "POST",
        data: { socket_id: socketId },
        success: function(data) {
            console.log("Succesful Ajax Call");
            console.log("Data: " + data);
        },
        error: function(error) {
            console.log("Bad Ajax Call");
        }
    });
});
pusher.connection.bind('unavailable', function (data) {
    console.log('unavailable');
});
pusher.connection.bind('failed', function (data) {
    console.log('failed');
});
pusher.connection.bind('disconnected', function (data) {
    console.log('disconnected');
});
console.log("State: " + pusher.connection.state);

});

由于响应以json形式返回,请尝试以下命令。

查看完整响应

alert(JSON.stringify(data));

只查看消息

alert( data.message);