Skip to content

Commit c9f97b4

Browse files
author
James Brundage
committed
feat: Get-WebSocket -Header ( Fixes #77 )
1 parent 20ea848 commit c9f97b4

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function Get-WebSocket {
1818
websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Watch |
1919
% {
2020
$_.commit.record.text
21-
}
21+
}
2222
.EXAMPLE
2323
# Watch BlueSky, but just the text and spacing
2424
$blueSkySocketUrl = "wss://jetstream2.us-$(
@@ -202,9 +202,14 @@ function Get-WebSocket {
202202

203203
# Any variables to declare in the WebSocket job.
204204
# These variables will also be added to the job as properties.
205-
[Collections.IDictionary]
205+
[Collections.IDictionary]
206206
$Variable = @{},
207207

208+
# Any Http Headers to include in the WebSocket request or server response.
209+
[Collections.IDictionary]
210+
[Alias('Headers')]
211+
$Header,
212+
208213
# The name of the WebSocket job.
209214
[string]
210215
$Name,
@@ -391,6 +396,11 @@ function Get-WebSocket {
391396
} else {
392397
$ws.Options.AddSubProtocol('json')
393398
}
399+
if ($Header) {
400+
foreach ($headerKeyValue in $header.GetEnumerator()) {
401+
$ws.Options.SetRequestHeader($headerKeyValue.Key, $headerKeyValue.Value)
402+
}
403+
}
394404
$null = $ws.ConnectAsync($SocketUrl, $CT).Wait()
395405
} else {
396406
$ws = $WebSocket
@@ -688,6 +698,16 @@ function Get-WebSocket {
688698
# and by default it will pass a message containing the context.
689699
$messageData = [Ordered]@{Protocol = $protocol; Url = $context.Request.Url;Context = $context}
690700

701+
if ($Header -and $response) {
702+
foreach ($headerKeyValue in $Header.GetEnumerator()) {
703+
try {
704+
$response.Headers.Add($headerKeyValue.Key, $headerKeyValue.Value)
705+
} catch {
706+
Write-Warning "Cannot add header '$($headerKeyValue.Key)': $_"
707+
}
708+
}
709+
}
710+
691711
# HttpListeners are quite nice, especially when it comes to websocket upgrades.
692712
# If the request is a websocket request
693713
if ($Request.IsWebSocketRequest) {

0 commit comments

Comments
 (0)