3 回答

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
有幾件事:
為什么不直接使用 nativescript-pusher 插件呢?它已經(jīng)存在了...
第二,如果你不想使用它;為什么不借用代碼,因?yàn)樗窃?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());
}
} );
首先,在創(chuàng)建事件時(shí),您確實(shí)應(yīng)該使用 FULL 命名空間(這使得創(chuàng)建的內(nèi)容一目了然)。
其次,你的原型onEvent是錯(cuò)誤的。根據(jù)文檔,它是Channel, Event, Data傳遞給它的參數(shù)。

TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊
SubscriptionEventListener
是一個(gè)接口,您應(yīng)該實(shí)現(xiàn)方法并將實(shí)例傳遞給綁定方法,如文檔中所示。
channel.bind("my-event",?
? ?new SubscriptionEventListener({
? ? onEvent: function(event) {
? ? ? ? console.log("Received event with data: " + event.toString());
? ? }
? ?})
);

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
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);
}
};
添加回答
舉報(bào)