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);
}
};
添加回答
舉報