Skip to content

Commit 123d554

Browse files
author
James Brundage
committed
feat: WebSocket reuse ( Fixes #35 )
1 parent 408967e commit 123d554

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function Get-WebSocket {
8484
{$args.commit.record.text -match '[\p{IsHighSurrogates}\p{IsLowSurrogates}]+'}={
8585
$matches.0
8686
}
87-
}
87+
}
8888
#>
8989
[CmdletBinding(PositionalBinding=$false)]
9090
[Alias('WebSocket')]
@@ -214,7 +214,7 @@ function Get-WebSocket {
214214

215215
if (-not $WebSocketUri.Scheme) {
216216
$WebSocketUri = [uri]"wss://$WebSocketUri"
217-
}
217+
}
218218

219219
if (-not $BufferSize) {
220220
$BufferSize = 16kb
@@ -306,8 +306,22 @@ function Get-WebSocket {
306306
if (-not $name) {
307307
$Name = $WebSocketUri
308308
}
309-
310-
Start-ThreadJob -ScriptBlock $SocketJob -Name $Name -InitializationScript $InitializationScript -ArgumentList $Variable
309+
310+
$existingJob = foreach ($jobWithThisName in (Get-Job -Name $Name)) {
311+
if (
312+
$jobWithThisName.State -in 'Running','NotStarted' -and
313+
$jobWithThisName.WebSocket -is [Net.WebSockets.ClientWebSocket]
314+
) {
315+
$jobWithThisName
316+
break
317+
}
318+
}
319+
320+
if ($existingJob) {
321+
$existingJob
322+
} else {
323+
Start-ThreadJob -ScriptBlock $SocketJob -Name $Name -InitializationScript $InitializationScript -ArgumentList $Variable
324+
}
311325
} elseif ($WebSocket) {
312326
if (-not $name) {
313327
$name = "websocket"

0 commit comments

Comments
 (0)