Skip to content

Commit df9ebb4

Browse files
author
James Brundage
committed
feat: Get-WebSocket -WatchFor ( Fixes #29 )
1 parent b11e8ee commit df9ebb4

File tree

1 file changed

+81
-4
lines changed

1 file changed

+81
-4
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function Get-WebSocket {
77
88
This will create a job that connects to a WebSocket and outputs the results.
99
10-
If the `-Watch` parameter is provided, will output a continous stream of objects from the websocket.
10+
If the `-Watch` parameter is provided, will output a continous stream of objects.
1111
.EXAMPLE
1212
# Create a WebSocket job that connects to a WebSocket and outputs the results.
1313
Get-WebSocket -WebSocketUri "wss://localhost:9669/"
@@ -58,6 +58,33 @@ function Get-WebSocket {
5858
Foreach-Object {
5959
$_.commit.record.embed.external.uri
6060
}
61+
.EXAMPLE
62+
# BlueSky, but just the hashtags
63+
websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -WatchFor @{
64+
{$webSocketoutput.commit.record.text -match "\#\w+"}={
65+
$matches.0
66+
}
67+
}
68+
.EXAMPLE
69+
# BlueSky, but just the hashtags (as links)
70+
websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -WatchFor @{
71+
{$webSocketoutput.commit.record.text -match "\#\w+"}={
72+
if ($psStyle.FormatHyperlink) {
73+
$psStyle.FormatHyperlink($matches.0, "https://bsky.app/search?q=$([Web.HttpUtility]::UrlEncode($matches.0))")
74+
} else {
75+
$matches.0
76+
}
77+
}
78+
}
79+
.EXAMPLE
80+
websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -WatchFor @{
81+
{$args.commit.record.text -match "\#\w+"}={
82+
$matches.0
83+
}
84+
{$args.commit.record.text -match '[\p{IsHighSurrogates}\p{IsLowSurrogates}]+'}={
85+
$matches.0
86+
}
87+
}
6188
#>
6289
[CmdletBinding(PositionalBinding=$false)]
6390
[Alias('WebSocket')]
@@ -72,6 +99,7 @@ function Get-WebSocket {
7299
$Handler,
73100

74101
# Any variables to declare in the WebSocket job.
102+
# These variables will also be added to the job as properties.
75103
[Collections.IDictionary]
76104
$Variable = @{},
77105

@@ -109,6 +137,28 @@ function Get-WebSocket {
109137
[switch]
110138
$Watch,
111139

140+
# If set, will watch the output of a WebSocket job for one or more conditions.
141+
# The conditions are the keys of the dictionary, and can be a regex, a string, or a scriptblock.
142+
# The values of the dictionary are what will happen when a match is found.
143+
[ValidateScript({
144+
$keys = $_.Keys
145+
$values = $_.values
146+
foreach ($key in $keys) {
147+
if ($key -isnot [scriptblock]) {
148+
throw "Keys '$key' must be a scriptblock"
149+
}
150+
}
151+
foreach ($value in $values) {
152+
if ($value -isnot [scriptblock] -and $value -isnot [string]) {
153+
throw "Value '$value' must be a string or scriptblock"
154+
}
155+
}
156+
return $true
157+
})]
158+
[Alias('WhereFor','Wherefore')]
159+
[Collections.IDictionary]
160+
$WatchFor,
161+
112162
# The timeout for the WebSocket connection. If this is provided, after the timeout elapsed, the WebSocket will be closed.
113163
[TimeSpan]
114164
$TimeOut,
@@ -167,8 +217,7 @@ function Get-WebSocket {
167217
$Variable.WebSocket = $ws
168218

169219
$MessageCount = [long]0
170-
171-
220+
172221
while ($true) {
173222
if ($ws.State -ne 'Open') {break }
174223
if ($TimeOut -and ([DateTime]::Now - $webSocketStartTime) -gt $TimeOut) {
@@ -275,7 +324,35 @@ function Get-WebSocket {
275324
7, 11, 13, 17, 19, 23 | Get-Random
276325
)
277326
} while ($webSocketJob.State -in 'Running','NotStarted')
278-
} else {
327+
}
328+
elseif ($WatchFor) {
329+
. {
330+
do {
331+
$webSocketJob | Receive-Job
332+
Start-Sleep -Milliseconds (
333+
7, 11, 13, 17, 19, 23 | Get-Random
334+
)
335+
} while ($webSocketJob.State -in 'Running','NotStarted')
336+
} | . {
337+
process {
338+
$webSocketOutput = $_
339+
foreach ($key in @($WatchFor.Keys)) {
340+
$result =
341+
if ($key -is [ScriptBlock]) {
342+
. $key $webSocketOutput
343+
}
344+
345+
if (-not $result) { continue }
346+
if ($WatchFor[$key] -is [ScriptBlock]) {
347+
$webSocketOutput | . $WatchFor[$key]
348+
} else {
349+
$WatchFor[$key]
350+
}
351+
}
352+
}
353+
}
354+
}
355+
else {
279356
$webSocketJob
280357
}
281358
}

0 commit comments

Comments
 (0)