繁体   English   中英

Pusher Ajax 调用似乎不起作用

[英]Pusher Ajax call doesn't seem to be working

第一次尝试使用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 });
    }

推送应用

$(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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM