flutter 使用laravel_echo设置pusher_client

kokeuurv  于 2023-10-22  发布在  Flutter
关注(0)|答案(2)|浏览(127)

我目前正在制作一个需要实时更新的应用程序,我使用pusher_client:Laravel_echo:^0.2.2.我创建了一个函数,在initState上调用:

PusherOptions options = PusherOptions(
      host: 'reservegcc.com',
      wsPort: 6001,
      encrypted: false,
    );

    PusherClient pusher = PusherClient('11111', options, autoConnect: false);
    Echo echo = Echo({
      'broadcaster': 'pusher',
      'client': pusher,
      "wsHost": 'reservegcc.com',
      "wsPort": 6001,
      "disableStats": true,
      'wssPort': 6001,
      'forceTLS': true,
      'enabledTransports': ['ws', 'wss']
    });
    echo.channel('restaurant-transactions').listen('Transaction', (e) {
      print(e);
    });
    echo.connector.pusher.onConnectionStateChange((state) {
      print(state!.currentState.toString());
    });
    echo.connector.pusher.connect();
  }```

Im getting connecting and reconning on the onConnectionStateChange, Can someone please help me what is lacking on my code. I have done a lot of research but still cannot connect.
r9f1avp5

r9f1avp51#

这对我来说非常好用(我使用laravel_flutter_pusher:^0.0.4):

void setUpServices() {
    var options = PusherOptions(
        host: '127.0.0.1',
        port: 6001,
        encrypted: false,
        cluster: 'pusher-cluster',
    );

    LaravelFlutterPusher pusher = LaravelFlutterPusher('pusher-key', options, enableLogging: true);
    pusher
        .subscribe('public')
        .bind('news', (event) => log('event =>$event'));
  }
vwkv1x7d

vwkv1x7d2#

兄弟,我花了很多时间在这个问题上,我发现了一个奇怪的问题,当你热重启/重新加载项目时,pusher包不会影响。因此,您必须退出并再次调试项目,然后查看更改的结果。
另一方面,我没有使用回声包,我写的通道和事件名称手动像这样;

_pusher.subscribe("private-user.call.$userId").bind(
  "App\\Events\\callUser",
  (PusherEvent event) {
    var data = jsonDecode(event.data);
    switch (data["type"]) {
      case "bla bla":
        // do something
        break;
      case "bla bla":
        // do something
        break;
    }
  },
);

相关问题