Skip to content

Commit

Permalink
feat: Support method streaming. (serverpod#2567)
Browse files Browse the repository at this point in the history
  • Loading branch information
SandPod authored Aug 6, 2024
1 parent b595b1a commit c048a22
Show file tree
Hide file tree
Showing 32 changed files with 2,735 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Client extends _i1.ServerpodClient {
StackTrace,
)? onFailedCall,
Function(_i1.MethodCallContext)? onSucceededCall,
bool? disconnectStreamsOnLostInternetConnection,
}) : super(
host,
_i4.Protocol(),
Expand All @@ -57,6 +58,8 @@ class Client extends _i1.ServerpodClient {
connectionTimeout: connectionTimeout,
onFailedCall: onFailedCall,
onSucceededCall: onSucceededCall,
disconnectStreamsOnLostInternetConnection:
disconnectStreamsOnLostInternetConnection,
) {
example = EndpointExample(this);
modules = _Modules(this);
Expand Down
3 changes: 3 additions & 0 deletions examples/chat/chat_client/lib/src/protocol/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Client extends _i1.ServerpodClient {
StackTrace,
)? onFailedCall,
Function(_i1.MethodCallContext)? onSucceededCall,
bool? disconnectStreamsOnLostInternetConnection,
}) : super(
host,
_i6.Protocol(),
Expand All @@ -63,6 +64,8 @@ class Client extends _i1.ServerpodClient {
connectionTimeout: connectionTimeout,
onFailedCall: onFailedCall,
onSucceededCall: onSucceededCall,
disconnectStreamsOnLostInternetConnection:
disconnectStreamsOnLostInternetConnection,
) {
channels = EndpointChannels(this);
modules = _Modules(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class MethodWebsocketRequestHandler {
);
return OpenMethodStreamResponse.buildMessage(
connectionId: message.connectionId,
endpoint: message.endpoint,
method: message.method,
responseType: OpenMethodStreamResponseType.endpointNotFound,
);
}
Expand All @@ -133,6 +135,8 @@ class MethodWebsocketRequestHandler {
);
return OpenMethodStreamResponse.buildMessage(
connectionId: message.connectionId,
endpoint: message.endpoint,
method: message.method,
responseType: OpenMethodStreamResponseType.endpointNotFound,
);
}
Expand All @@ -142,6 +146,8 @@ class MethodWebsocketRequestHandler {
'Endpoint method is not a valid stream method: $message',
);
return OpenMethodStreamResponse.buildMessage(
endpoint: message.endpoint,
method: message.method,
connectionId: message.connectionId,
responseType: OpenMethodStreamResponseType.endpointNotFound,
);
Expand All @@ -160,6 +166,8 @@ class MethodWebsocketRequestHandler {
'Failed to parse parameters for open stream request: $message',
);
return OpenMethodStreamResponse.buildMessage(
endpoint: message.endpoint,
method: message.method,
connectionId: message.connectionId,
responseType: OpenMethodStreamResponseType.invalidArguments,
);
Expand All @@ -176,6 +184,8 @@ class MethodWebsocketRequestHandler {
'Failed to parse input streams for open stream request: $message',
);
return OpenMethodStreamResponse.buildMessage(
endpoint: message.endpoint,
method: message.method,
connectionId: message.connectionId,
responseType: OpenMethodStreamResponseType.invalidArguments,
);
Expand Down Expand Up @@ -206,11 +216,15 @@ class MethodWebsocketRequestHandler {
return switch (authFailed.reason) {
AuthenticationFailureReason.insufficientAccess =>
OpenMethodStreamResponse.buildMessage(
endpoint: message.endpoint,
method: message.method,
connectionId: message.connectionId,
responseType: OpenMethodStreamResponseType.authorizationDeclined,
),
AuthenticationFailureReason.unauthenticated =>
OpenMethodStreamResponse.buildMessage(
endpoint: message.endpoint,
method: message.method,
connectionId: message.connectionId,
responseType: OpenMethodStreamResponseType.authenticationFailed,
),
Expand All @@ -228,6 +242,8 @@ class MethodWebsocketRequestHandler {
);

return OpenMethodStreamResponse.buildMessage(
endpoint: message.endpoint,
method: message.method,
connectionId: message.connectionId,
responseType: OpenMethodStreamResponseType.success,
);
Expand Down Expand Up @@ -574,6 +590,8 @@ class _MethodStreamManager {
required OpenMethodStreamCommand message,
required Server server,
}) {
/// Will never be stopped listened to if the method does not return.
/// This is a potential memory leak.
methodConnector.call(session, args, streamParams).listen(
(value) {
_postMessage(
Expand Down
Loading

0 comments on commit c048a22

Please sign in to comment.