Skip to content

Commit 335d7e9

Browse files
author
James Brundage
committed
feat: Get-WebSocket -Debug ( Fixes #45 )
1 parent ab00d9d commit 335d7e9

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ function Get-WebSocket {
178178
foreach ($key in $keys) {
179179
if ($key -isnot [scriptblock]) {
180180
throw "Keys '$key' must be a scriptblock"
181-
}
181+
}
182182
}
183183
foreach ($value in $values) {
184184
if ($value -isnot [scriptblock] -and $value -isnot [string]) {
@@ -224,13 +224,18 @@ function Get-WebSocket {
224224

225225
begin {
226226
$SocketJob = {
227-
param([Collections.IDictionary]$Variable)
227+
param(
228+
# By accepting a single parameter containing variables,
229+
# we can avoid the need to pass in a large number of parameters.
230+
[Collections.IDictionary]$Variable
231+
)
228232

233+
# Take every every `-Variable` passed in and define it within the job
229234
foreach ($keyValue in $variable.GetEnumerator()) {
230235
$ExecutionContext.SessionState.PSVariable.Set($keyValue.Key, $keyValue.Value)
231236
}
232237

233-
if ((-not $WebSocketUri) -or $webSocket) {
238+
if ((-not $WebSocketUri)) {
234239
throw "No WebSocketUri"
235240
}
236241

@@ -244,7 +249,7 @@ function Get-WebSocket {
244249

245250
$CT = [Threading.CancellationToken]::None
246251

247-
if (-not $webSocket) {
252+
if ($webSocket -isnot [Net.WebSockets.ClientWebSocket]) {
248253
$ws = [Net.WebSockets.ClientWebSocket]::new()
249254
if ($SubProtocol) {
250255
$ws.Options.AddSubProtocol($SubProtocol)
@@ -328,6 +333,10 @@ function Get-WebSocket {
328333
foreach ($keyValuePair in $PSBoundParameters.GetEnumerator()) {
329334
$Variable[$keyValuePair.Key] = $keyValuePair.Value
330335
}
336+
if ($DebugPreference -notin 'SilentlyContinue','Ignore') {
337+
. $SocketJob -Variable $Variable
338+
return
339+
}
331340
$webSocketJob =
332341
if ($WebSocketUri) {
333342
if (-not $name) {
@@ -362,32 +371,37 @@ function Get-WebSocket {
362371
SupportEvent = $true
363372
}
364373
$eventSubscriptions = @(
365-
if ($OnOutput) {
366-
Register-ObjectEvent @subscriptionSplat -InputObject $webSocketJob.Output -Action $OnOutput
367-
}
368-
if ($OnError) {
369-
Register-ObjectEvent @subscriptionSplat -InputObject $webSocketJob.Error -Action $OnError
370-
}
371-
if ($OnWarning) {
372-
Register-ObjectEvent @subscriptionSplat -InputObject $webSocketJob.Warning -Action $OnWarning
373-
}
374+
if ($webSocketJob) {
375+
if ($OnOutput) {
376+
Register-ObjectEvent @subscriptionSplat -InputObject $webSocketJob.Output -Action $OnOutput
377+
}
378+
if ($OnError) {
379+
Register-ObjectEvent @subscriptionSplat -InputObject $webSocketJob.Error -Action $OnError
380+
}
381+
if ($OnWarning) {
382+
Register-ObjectEvent @subscriptionSplat -InputObject $webSocketJob.Warning -Action $OnWarning
383+
}
384+
}
374385
)
375386
if ($eventSubscriptions) {
376387
$variable['EventSubscriptions'] = $eventSubscriptions
377388
}
378389

379-
$webSocketConnectTimeout = [DateTime]::Now + $ConnectionTimeout
380-
while (-not $variable['WebSocket'] -and
381-
([DateTime]::Now -lt $webSocketConnectTimeout)) {
382-
Start-Sleep -Milliseconds 0
383-
}
390+
if ($webSocketJob) {
391+
$webSocketConnectTimeout = [DateTime]::Now + $ConnectionTimeout
392+
while (-not $variable['WebSocket'] -and
393+
([DateTime]::Now -lt $webSocketConnectTimeout)) {
394+
Start-Sleep -Milliseconds 0
395+
}
396+
397+
foreach ($keyValuePair in $Variable.GetEnumerator()) {
398+
$webSocketJob.psobject.properties.add(
399+
[psnoteproperty]::new($keyValuePair.Key, $keyValuePair.Value), $true
400+
)
401+
}
402+
$webSocketJob.pstypenames.insert(0, 'WebSocketJob')
403+
}
384404

385-
foreach ($keyValuePair in $Variable.GetEnumerator()) {
386-
$webSocketJob.psobject.properties.add(
387-
[psnoteproperty]::new($keyValuePair.Key, $keyValuePair.Value), $true
388-
)
389-
}
390-
$webSocketJob.pstypenames.insert(0, 'WebSocketJob')
391405
if ($Watch) {
392406
do {
393407
$webSocketJob | Receive-Job

0 commit comments

Comments
 (0)