@@ -7,7 +7,7 @@ function Get-WebSocket {
7
7
8
8
This will create a job that connects to a WebSocket and outputs the results.
9
9
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.
11
11
. EXAMPLE
12
12
# Create a WebSocket job that connects to a WebSocket and outputs the results.
13
13
Get-WebSocket -WebSocketUri "wss://localhost:9669/"
@@ -58,6 +58,33 @@ function Get-WebSocket {
58
58
Foreach-Object {
59
59
$_.commit.record.embed.external.uri
60
60
}
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
+ }
61
88
#>
62
89
[CmdletBinding (PositionalBinding = $false )]
63
90
[Alias (' WebSocket' )]
@@ -72,6 +99,7 @@ function Get-WebSocket {
72
99
$Handler ,
73
100
74
101
# Any variables to declare in the WebSocket job.
102
+ # These variables will also be added to the job as properties.
75
103
[Collections.IDictionary ]
76
104
$Variable = @ {},
77
105
@@ -109,6 +137,28 @@ function Get-WebSocket {
109
137
[switch ]
110
138
$Watch ,
111
139
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
+
112
162
# The timeout for the WebSocket connection. If this is provided, after the timeout elapsed, the WebSocket will be closed.
113
163
[TimeSpan ]
114
164
$TimeOut ,
@@ -167,8 +217,7 @@ function Get-WebSocket {
167
217
$Variable.WebSocket = $ws
168
218
169
219
$MessageCount = [long ]0
170
-
171
-
220
+
172
221
while ($true ) {
173
222
if ($ws.State -ne ' Open' ) {break }
174
223
if ($TimeOut -and ([DateTime ]::Now - $webSocketStartTime ) -gt $TimeOut ) {
@@ -275,7 +324,35 @@ function Get-WebSocket {
275
324
7 , 11 , 13 , 17 , 19 , 23 | Get-Random
276
325
)
277
326
} 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 {
279
356
$webSocketJob
280
357
}
281
358
}
0 commit comments