haoguo / Rocket.Chat.Dart.SDK

MIT License
32 stars 23 forks source link

Need await on ddp connect #5

Open MohamedAmineB opened 5 years ago

MohamedAmineB commented 5 years ago

Hi, thank you very much for this work. I tried to use your realtime Client, as is, but when I create a new Client, as I saw on your test example Client client = Client(..., true); and then I try to login

client.login(UserCredentials()
      ..name = 'admin'
      ..password = 'admin');

I get an error saying ddp is null To fix this I had to extract the connection bloc of code from the Client constructor and put this lines in a new async method

void connect({String wsUrl, bool logOrNot: false}) async {
    this._ddp = await ddp.DdpClient("", wsUrl, "");
    await this._ddp.connect();
    await this._ddp.setSocketLogActive(logOrNot);
  }

then call this new connection method with an await prefix

Am I misusing your code? Or should it be corrected?

Thx

allanwolski commented 4 years ago

@MohamedAmineB, you can use the addStatusListener method to know when the client is connected.

Client client = new Client(..., true);

client.addStatusListener((status) {
   if (status == ConnectStatus.connected) {
      client.login(UserCredentials()
         ..name = 'admin'
         ..password = 'admin');
   }
});