Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added closed connection callback #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/duplex_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract class DuplexConnection implements Closeable, Availability {

typedef TcpChunkHandler = void Function(Uint8List chunk);
typedef CloseHandler = void Function();
typedef SocketClosedCallback = void Function();

class TcpDuplexConnection extends DuplexConnection {
Socket socket;
Expand Down Expand Up @@ -60,8 +61,9 @@ class TcpDuplexConnection extends DuplexConnection {
class WebSocketDuplexConnection extends DuplexConnection {
WebSocketChannel webSocket;
bool closed = true;
SocketClosedCallback? socketClosedCallback;

WebSocketDuplexConnection(this.webSocket);
WebSocketDuplexConnection(this.webSocket, {this.socketClosedCallback});

@override
void init() {
Expand All @@ -84,6 +86,7 @@ class WebSocketDuplexConnection extends DuplexConnection {
closed = true;
_availability = 0.0;
closeHandler?.call();
socketClosedCallback?.call();
}
}

Expand All @@ -94,7 +97,7 @@ class WebSocketDuplexConnection extends DuplexConnection {
}
}

Future<DuplexConnection> connectRSocket(String url, TcpChunkHandler handler) {
Future<DuplexConnection> connectRSocket(String url, TcpChunkHandler handler,SocketClosedCallback? socketClosedCallback) {
var uri = Uri.parse(url);
var scheme = uri.scheme;
if (scheme == 'tcp') {
Expand All @@ -104,7 +107,7 @@ Future<DuplexConnection> connectRSocket(String url, TcpChunkHandler handler) {
final websocket = WebSocketChannel.connect(
Uri.parse(url),
);
return Future.value(WebSocketDuplexConnection(websocket));
return Future.value(WebSocketDuplexConnection(websocket, socketClosedCallback: socketClosedCallback));
} else {
return Future.error('${scheme} unsupported');
}
Expand Down
8 changes: 7 additions & 1 deletion lib/rsocket_connector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class RSocketConnector {
String _metadataMimeType = 'message/x.rsocket.composite-metadata.v0';
ErrorConsumer? _errorConsumer;
SocketAcceptor? _acceptor;
SocketClosedCallback? _socketClosedCallback;

RSocketConnector.create();

Expand All @@ -22,6 +23,11 @@ class RSocketConnector {
return this;
}

RSocketConnector setupClosedCallback(SocketClosedCallback socketClosedCallback) {
_socketClosedCallback = socketClosedCallback;
return this;
}

RSocketConnector setupPayload(Payload payload) {
this.payload = payload;
return this;
Expand Down Expand Up @@ -53,7 +59,7 @@ class RSocketConnector {
..dataMimeType = _dataMimeType
..data = payload?.data
..metadata = payload?.metadata;
return connectRSocket(url, handler).then((conn) {
return connectRSocket(url, handler, _socketClosedCallback).then((conn) {
var rsocketRequester =
RSocketRequester('requester', connectionSetupPayload, conn);
if (_acceptor != null) {
Expand Down