Skip to content

Commit

Permalink
Make filterConnections a futureor
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Sep 8, 2024
1 parent c9c0f15 commit 81fcf2b
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/networker/networker_socket/lib/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class NetworkerSocketServer extends NetworkerServer<NetworkerSocketInfo> {
final SecurityContext? securityContext;
final dynamic serverAddress;
final int port;
bool Function(HttpRequest event)? filterConnections;
FutureOr<bool> Function(HttpRequest event)? filterConnections;
final bool overrideStatusCode;

HttpServer? get server => _server;

Expand All @@ -40,6 +41,7 @@ class NetworkerSocketServer extends NetworkerServer<NetworkerSocketInfo> {
this.port, {
this.filterConnections,
this.securityContext,
this.overrideStatusCode = true,
});

final StreamController<void> _onOpen = StreamController<void>.broadcast(),
Expand Down Expand Up @@ -68,8 +70,16 @@ class NetworkerSocketServer extends NetworkerServer<NetworkerSocketInfo> {
);

void _run() {
_server?.where(filterConnections ?? (e) => true).listen((request) async {
_server?.listen((request) async {
try {
if (await filterConnections?.call(request) == false) {
if (overrideStatusCode) {
request.response.statusCode = HttpStatus.forbidden;
}
request.response.close();
return;
}
request.response.statusCode = HttpStatus.switchingProtocols;
final socket = await WebSocketTransformer.upgrade(request);
final info = request.connectionInfo;
final id = addClientConnection(NetworkerSocketInfo(
Expand Down

0 comments on commit 81fcf2b

Please sign in to comment.