初始化套接字时,请添加enableForceNew((函数。
我正在使用https://pub.dev/packages/socket_io_client在flutter应用程序中。但当用户注销时,我想断开与服务器的连接,但这并没有发生。
class WebsocketService {
// final Socket? socket;
IO.Socket? chatSocket;
WebsocketService()
: chatSocket =
IO.io("$Server/${SocketConstants.ChatNameSpace}", <String, dynamic>{
'transports': <String>['websocket'],
'query': <String, dynamic>{
"access_token": token,
}
});
void logout() {
debugPrint("will logout socket");
chatSocket?.clearListeners();
chatSocket?.destroy();
chatSocket?.dispose();
chatSocket!.disconnect();
chatSocket?.io.disconnect();
chatSocket!.io.close();
chatSocket!.io.destroy(chatSocket);
}
}