Skip to content

Commit

Permalink
feat: WebSocket reuse ( Fixes #35 )
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Dec 20, 2024
1 parent 408967e commit 123d554
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions Commands/Get-WebSocket.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function Get-WebSocket {
{$args.commit.record.text -match '[\p{IsHighSurrogates}\p{IsLowSurrogates}]+'}={
$matches.0
}
}
}
#>
[CmdletBinding(PositionalBinding=$false)]
[Alias('WebSocket')]
Expand Down Expand Up @@ -214,7 +214,7 @@ function Get-WebSocket {

if (-not $WebSocketUri.Scheme) {
$WebSocketUri = [uri]"wss://$WebSocketUri"
}
}

if (-not $BufferSize) {
$BufferSize = 16kb
Expand Down Expand Up @@ -306,8 +306,22 @@ function Get-WebSocket {
if (-not $name) {
$Name = $WebSocketUri
}

Start-ThreadJob -ScriptBlock $SocketJob -Name $Name -InitializationScript $InitializationScript -ArgumentList $Variable

$existingJob = foreach ($jobWithThisName in (Get-Job -Name $Name)) {
if (
$jobWithThisName.State -in 'Running','NotStarted' -and
$jobWithThisName.WebSocket -is [Net.WebSockets.ClientWebSocket]
) {
$jobWithThisName
break
}
}

if ($existingJob) {
$existingJob
} else {
Start-ThreadJob -ScriptBlock $SocketJob -Name $Name -InitializationScript $InitializationScript -ArgumentList $Variable
}
} elseif ($WebSocket) {
if (-not $name) {
$name = "websocket"
Expand Down

0 comments on commit 123d554

Please sign in to comment.