Open cfanboy opened 3 years ago
Hi, I updated the package, you can upgrade to version 1.0.0-alpha.4
. It must work fine now.
You also don't need to create an instance every time. You can simply use disconnect
and connect
method.
Thanks. I updated and it's working.
Do you mean no need to set the tinode = null
when calling disconnect every time?
Yes, you can keep the instance and just call disconnect
and connect
method
I tried only to call connect
and disconnect
this way before but hit a wired problem.
MyChats page list all users who chat with me. My initialize flow is:
connect
tinode.loginToken
to login -> Subscribe me
topicinitState
, listen to messages:
tinode.onMessage.listen((value) {
// process received Meta message
});
If the app startup the first time, the tinode
instance is null. It's working fine, can list the users who talked with me.
Then I log out, calling tinode.disconnect
, and login again, call connect
method, wired thing happened, sometimes the send message very slowly, like loginToken
costs over 3 seconds, and the message listener callback never working, I can see the IM Server response the query result {Meta} message, but app cannot go into onMessage
method.
So I tried to set the tinode = null and found the above issue. Below is my WebSocketUtils codes:
class WebSocketUtils {
static WebSocketUtils _singleton;
Tinode tinode;
static WebSocketUtils getInstance() {
if (_singleton == null) {
_singleton = new WebSocketUtils();
}
return _singleton;
}
connectService({String hostUrl}) async {
try {
if (tinode == null)
tinode = Tinode(
'Mitao',
ConnectionOptions(WebSocketUtils.WsUrl, WebSocketUtils.ApiKey, secure: true),
ApiConstant.isDebug);
CtrlMessage connectResult = await tinode.connect();
if (connectResult.code < 300) {
isConnect = true;
}
} catch (error) {
print(error);
}
}
login() async {
if (tinode == null || tinode.isConnected == false) {
await connectService();
}
String imToken = await SharedPreferenceUtils.getValue(SharedPreferenceUtils.KEY_IM_TOKEN);
CtrlMessage loginResult = await tinode.loginToken(imToken, null).then((value) {
LogUtil.v(value);
return value;
}, onError: (error) {
LogUtil.e(error);
return null;
});
await tinode.subscribe("me", null, new SetParams()).then((value) {
LogUtil.v(value);
return value;
}, onError: (error) {
LogUtil.e(error);
return null;
});
return loginResult;
}
disConnect() {
tinode.disconnect();
}
}
OK thank you for describing the problem I'll investigate this and see if I can find the problem. So for now let's initialize tinode each time you want to log in.
Ok, thank you again. ^_^
I just did testing. The messager listener as below did not work on the second the message sent.
var me = WebSocketUtils.getInstance().tinode.getMeTopic(); me.onSubsUpdated.listen((value) async { // Process received data }
sent: {"get":{"topic":"me","what":"sub","id":"115639"}}
Thanks I'll see what can I do
Hi, I'm coming again. I initialized the Tincode as below codes on app startup:
When the user logout, call the disconnect method and set the tinode instance to null:
But after the user signs in again, I re-call the initialization method, it throws the error:
I'm not sure what's problem is, is my disconnect not correct? Thanks