Skip to content

Commit

Permalink
feat: Get-WebSocket is aliased to ws and wss ( Fixes #57 )
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Jan 9, 2025
1 parent 17b61d3 commit b6a90c7
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions Commands/Get-WebSocket.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,20 @@ function Get-WebSocket {
Select -First 10
#>
[CmdletBinding(PositionalBinding=$false,SupportsPaging)]
[Alias('WebSocket')]
[Alias('WebSocket','ws','wss')]
param(
# The Uri of the WebSocket to connect to.
# The WebSocket Uri.
[Parameter(Position=0,ValueFromPipelineByPropertyName)]
[Alias('Url','Uri')]
[uri]$WebSocketUri,

# One or more root urls.
# If these are provided, a WebSocket server will be created with these listener prefixes.
[Parameter(Position=1,ValueFromPipelineByPropertyName)]
[Alias('HostHeader','Host','ServerURL','ListenerPrefix','ListenerPrefixes','ListenerUrl')]
[string[]]
$RootUrl,

# A collection of query parameters.
# These will be appended onto the `-WebSocketUri`.
[Collections.IDictionary]
Expand Down Expand Up @@ -238,7 +245,7 @@ function Get-WebSocket {
)

begin {
$SocketJob = {
$SocketClientJob = {
param(
# By accepting a single parameter containing variables,
# we can avoid the need to pass in a large number of parameters.
Expand All @@ -251,6 +258,8 @@ function Get-WebSocket {
$ExecutionContext.SessionState.PSVariable.Set($keyValue.Key, $keyValue.Value)
}

$Variable.JobRunspace = [Runspace]::DefaultRunspace

if ((-not $WebSocketUri)) {
throw "No WebSocketUri"
}
Expand Down Expand Up @@ -288,7 +297,7 @@ function Get-WebSocket {
$null = $ws.ConnectAsync($WebSocketUri, $CT).Wait()
} else {
$ws = $WebSocket
}
}

$webSocketStartTime = $Variable.WebSocketStartTime = [DateTime]::Now
$Variable.WebSocket = $ws
Expand Down Expand Up @@ -394,7 +403,7 @@ function Get-WebSocket {
Write-Error $_
}
}
}
}
}

process {
Expand All @@ -405,7 +414,7 @@ function Get-WebSocket {
# If `-Debug` was passed,
if ($DebugPreference -notin 'SilentlyContinue','Ignore') {
# run the job in the current scope (so we can debug it).
. $SocketJob -Variable $Variable
. $SocketClientJob -Variable $Variable
return
}
$webSocketJob =
Expand All @@ -427,13 +436,8 @@ function Get-WebSocket {
if ($existingJob) {
$existingJob
} else {
Start-ThreadJob -ScriptBlock $SocketJob -Name $Name -InitializationScript $InitializationScript -ArgumentList $Variable
Start-ThreadJob -ScriptBlock $SocketClientJob -Name $Name -InitializationScript $InitializationScript -ArgumentList $Variable
}
} elseif ($WebSocket) {
if (-not $name) {
$name = "websocket"
}
Start-ThreadJob -ScriptBlock $SocketJob -Name $Name -InitializationScript $InitializationScript -ArgumentList $Variable
}

$subscriptionSplat = @{
Expand Down Expand Up @@ -470,7 +474,7 @@ function Get-WebSocket {
[psnoteproperty]::new($keyValuePair.Key, $keyValuePair.Value), $true
)
}
$webSocketJob.pstypenames.insert(0, 'WebSocketJob')
$webSocketJob.pstypenames.insert(0, 'WebSocket.ThreadJob')
}

if ($Watch) {
Expand Down

0 comments on commit b6a90c7

Please sign in to comment.