@@ -670,9 +670,14 @@ function Get-WebSocket {
670
670
# Otherwise, we'll just run the handler.
671
671
$webSocketMessage | . $handler
672
672
}
673
- } else {
674
- $webSocketMessage
675
673
}
674
+
675
+ # If we have a response from the handler,
676
+ if ($handledResponse ) {
677
+ $handledResponse # emit that response.
678
+ } else {
679
+ $webSocketMessage # otherwise, emit the message.
680
+ }
676
681
} catch {
677
682
Write-Error $_
678
683
}
@@ -897,7 +902,7 @@ function Get-WebSocket {
897
902
898
903
# Now add the result it to the SocketRequests lookup table, using the request trace identifier as the key.
899
904
$clientBuffer = $webSocketResult.WebSocket ::CreateClientBuffer($BufferSize , $BufferSize )
900
- $httpListener .SocketRequests [ $context .Request.RequestTraceIdentifier ] = [Ordered ]@ {
905
+ $socketObject = [ PSCustomObject ] [Ordered ]@ {
901
906
Context = $context
902
907
WebSocketContext = $webSocketResult
903
908
WebSocket = $webSocketResult.WebSocket
@@ -908,6 +913,10 @@ function Get-WebSocket {
908
913
MessageQueue = [Collections.Queue ]::new()
909
914
MessageCount = [long ]0
910
915
}
916
+ if (-not $httpListener.SocketRequests [" $ ( $webSocketResult.RequestUri ) " ]) {
917
+ $httpListener.SocketRequests [" $ ( $webSocketResult.RequestUri ) " ] = [Collections.Queue ]::new()
918
+ }
919
+ $httpListener.SocketRequests [" $ ( $webSocketResult.RequestUri ) " ].Enqueue($socketObject )
911
920
# and add the websocketcontext result to the message data.
912
921
$messageData [" WebSocketContext" ] = $webSocketResult
913
922
# also add the websocket result to the message data,
@@ -1157,7 +1166,10 @@ function Get-WebSocket {
1157
1166
[psnoteproperty ]::new($keyValuePair.Key , $keyValuePair.Value ), $true
1158
1167
)
1159
1168
}
1160
- $httpListenerJob
1169
+
1170
+ if (-not $Broadcast ) {
1171
+ $httpListenerJob
1172
+ }
1161
1173
}
1162
1174
1163
1175
if ($Broadcast ) {
@@ -1168,7 +1180,7 @@ function Get-WebSocket {
1168
1180
$broadcast = [ArraySegment [byte ]]::new($broadcast )
1169
1181
}
1170
1182
if ($broadcast -is [System.ArraySegment [byte ]]) {
1171
- foreach ($socketRequest in $httpListener.SocketRequests.Values ) {
1183
+ foreach ($socketRequest in @ ( $httpListener.SocketRequests.Values ) ) {
1172
1184
$socketRequest.WebSocket.SendAsync ($broadcast , ' Binary' , ' EndOfMessage' , [Threading.CancellationToken ]::None)
1173
1185
}
1174
1186
}
@@ -1177,7 +1189,7 @@ function Get-WebSocket {
1177
1189
$broadcastJson = ConvertTo-Json - InputObject $broadcastItem
1178
1190
$broadcastJsonBytes = $OutputEncoding.GetBytes ($broadcastJson )
1179
1191
$broadcastSegment = [ArraySegment [byte ]]::new($broadcastJsonBytes )
1180
- foreach ($socketRequest in $httpListener.SocketRequests.Values ) {
1192
+ foreach ($socketRequest in @ ( $httpListener.SocketRequests.Values | . { process { $_ } }) ) {
1181
1193
$socketRequest.WebSocket.SendAsync ($broadcastSegment , ' Text' , ' EndOfMessage' , [Threading.CancellationToken ]::None)
1182
1194
}
1183
1195
}
0 commit comments