하트비트 추가
This commit is contained in:
parent
cc94c5618d
commit
d2f5f854ee
1 changed files with 16 additions and 2 deletions
|
|
@ -2,6 +2,7 @@
|
|||
// ignore_for_file: avoid_init_to_null, prefer_final_fields, avoid_print
|
||||
|
||||
import 'dart:io';
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:dart_framework/utils/system_util.dart';
|
||||
|
|
@ -12,14 +13,17 @@ import 'package:dart_framework/socket/packets/message_common.pb.dart';
|
|||
abstract class ProtobufClient extends Communicator
|
||||
{
|
||||
final int _headerSize = 4;
|
||||
final int _heartbeatBuffer = 10;
|
||||
final int _heartbeatTime;
|
||||
final Socket _socket;
|
||||
late Timer? _heartbeatTimer = null;
|
||||
int? _length = null;
|
||||
bool _isAlive = false;
|
||||
late List<int> _arrivedData = [];
|
||||
List<void Function(ProtobufClient)> _onDisconnectListenerList = [];
|
||||
|
||||
|
||||
ProtobufClient(this._socket, Map<String, GeneratedMessage Function(List<int>)> parserMap)
|
||||
ProtobufClient(this._socket, this._heartbeatTime, Map<String, GeneratedMessage Function(List<int>)> parserMap)
|
||||
{
|
||||
print('Connected New Client');
|
||||
_isAlive = true;
|
||||
|
|
@ -31,11 +35,20 @@ abstract class ProtobufClient extends Communicator
|
|||
addListener(onHeartBeat);
|
||||
_socket.listen(onData, onError: onError)
|
||||
.asFuture().then(onDisconnected);
|
||||
// waitHeartbeat();
|
||||
}
|
||||
|
||||
void waitHeartbeat()
|
||||
{
|
||||
_heartbeatTimer = Timer(Duration(seconds: _heartbeatTime + _heartbeatBuffer), () {
|
||||
dispose();
|
||||
});
|
||||
}
|
||||
|
||||
void onHeartBeat(HeartBeat data)
|
||||
{
|
||||
|
||||
_heartbeatTimer?.cancel();
|
||||
waitHeartbeat();
|
||||
}
|
||||
|
||||
void addDisconnectListener(void Function(ProtobufClient) handler)
|
||||
|
|
@ -129,6 +142,7 @@ abstract class ProtobufClient extends Communicator
|
|||
|
||||
void dispose()
|
||||
{
|
||||
_isAlive = false;
|
||||
_socket.close();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue