Skip to content

Commit a03570b

Browse files
author
James Brundage
committed
fix: Get-WebSocket output docs ( Fixes #82 )
Documenting and fixing output
1 parent b62a4d0 commit a03570b

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,14 @@ function Get-WebSocket {
670670
# Otherwise, we'll just run the handler.
671671
$webSocketMessage | . $handler
672672
}
673-
} else {
674-
$webSocketMessage
675673
}
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+
}
676681
} catch {
677682
Write-Error $_
678683
}
@@ -897,7 +902,7 @@ function Get-WebSocket {
897902

898903
# Now add the result it to the SocketRequests lookup table, using the request trace identifier as the key.
899904
$clientBuffer = $webSocketResult.WebSocket::CreateClientBuffer($BufferSize, $BufferSize)
900-
$httpListener.SocketRequests[$context.Request.RequestTraceIdentifier] = [Ordered]@{
905+
$socketObject = [PSCustomObject][Ordered]@{
901906
Context = $context
902907
WebSocketContext = $webSocketResult
903908
WebSocket = $webSocketResult.WebSocket
@@ -908,6 +913,10 @@ function Get-WebSocket {
908913
MessageQueue = [Collections.Queue]::new()
909914
MessageCount = [long]0
910915
}
916+
if (-not $httpListener.SocketRequests["$($webSocketResult.RequestUri)"]) {
917+
$httpListener.SocketRequests["$($webSocketResult.RequestUri)"] = [Collections.Queue]::new()
918+
}
919+
$httpListener.SocketRequests["$($webSocketResult.RequestUri)"].Enqueue($socketObject)
911920
# and add the websocketcontext result to the message data.
912921
$messageData["WebSocketContext"] = $webSocketResult
913922
# also add the websocket result to the message data,
@@ -1157,7 +1166,10 @@ function Get-WebSocket {
11571166
[psnoteproperty]::new($keyValuePair.Key, $keyValuePair.Value), $true
11581167
)
11591168
}
1160-
$httpListenerJob
1169+
1170+
if (-not $Broadcast) {
1171+
$httpListenerJob
1172+
}
11611173
}
11621174

11631175
if ($Broadcast) {
@@ -1168,7 +1180,7 @@ function Get-WebSocket {
11681180
$broadcast = [ArraySegment[byte]]::new($broadcast)
11691181
}
11701182
if ($broadcast -is [System.ArraySegment[byte]]) {
1171-
foreach ($socketRequest in $httpListener.SocketRequests.Values) {
1183+
foreach ($socketRequest in @($httpListener.SocketRequests.Values)) {
11721184
$socketRequest.WebSocket.SendAsync($broadcast, 'Binary', 'EndOfMessage', [Threading.CancellationToken]::None)
11731185
}
11741186
}
@@ -1177,7 +1189,7 @@ function Get-WebSocket {
11771189
$broadcastJson = ConvertTo-Json -InputObject $broadcastItem
11781190
$broadcastJsonBytes = $OutputEncoding.GetBytes($broadcastJson)
11791191
$broadcastSegment = [ArraySegment[byte]]::new($broadcastJsonBytes)
1180-
foreach ($socketRequest in $httpListener.SocketRequests.Values) {
1192+
foreach ($socketRequest in @($httpListener.SocketRequests.Values | . { process { $_ } })) {
11811193
$socketRequest.WebSocket.SendAsync($broadcastSegment, 'Text', 'EndOfMessage', [Threading.CancellationToken]::None)
11821194
}
11831195
}

0 commit comments

Comments
 (0)