3 回答
TA贡献1860条经验 获得超8个赞
有几件事:
为什么不直接使用 nativescript-pusher 插件呢?它已经存在了...
第二,如果你不想使用它;为什么不借用代码,因为它是在 Apache 2.0 许可证下的。
不过,要具体回答你的问题:
const sel = new com.pusher.client.channel.SubscriptionEventListener( {
onEvent: function(channel, event, data) {
console.log("Channel:", channel, "Event", event, "received event with data: " + data.toString());
}
} );
首先,在创建事件时,您确实应该使用 FULL 命名空间(这使得创建的内容一目了然)。
其次,你的原型onEvent是错误的。根据文档,它是Channel, Event, Data传递给它的参数。
TA贡献1891条经验 获得超3个赞
SubscriptionEventListener
是一个接口,您应该实现方法并将实例传递给绑定方法,如文档中所示。
channel.bind("my-event",
new SubscriptionEventListener({
onEvent: function(event) {
console.log("Received event with data: " + event.toString());
}
})
);
TA贡献1884条经验 获得超4个赞
module.exports = {
connect:function(app_key, channel_name, event_name) {
PusherOptions = com.pusher.client.PusherOptions;
Pusher = com.pusher.client.Pusher;
Channel = com.pusher.client.channel.Channel;
PusherEvent = com.pusher.client.channel.PusherEvent;
SubscriptionEventListener = com.pusher.client.channel.SubscriptionEventListener;
ChannelEventListener = com.pusher.client.channel.ChannelEventListener;
const options = new PusherOptions().setCluster("eu");
const pusher = new Pusher(app_key, options);
pusher.connect();
const channel = new Channel(pusher.subscribe(channel_name));
const connectedChannel = pusher.getChannel(channel_name);
let sel = new SubscriptionEventListener({
onEvent: function(event) {
console.log(event);
}
});
connectedChannel.bind(event_name, sel);
}
};
添加回答
举报