Skip to content

Commit 4ef241e

Browse files
author
James Brundage
committed
feat: Get-WebSocket -Handshake ( Fixes #81 )
1 parent a45590d commit 4ef241e

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,15 @@ function Get-WebSocket {
256256
[Alias('Authorize','HelloMessage')]
257257
[PSObject]
258258
$Authenticate,
259-
260-
[Alias('Identify','AutoReply')]
259+
260+
# If provided, will shake hands after the first websocket message is received.
261+
# This parameter can be either a ScriptBlock or any other object.
262+
# If it is a ScriptBlock, it will be run with the output of the WebSocket passed as the first argument.
263+
# This will run after the socket is connected and the first message is received.
264+
[Parameter(ParameterSetName='WebSocketClient')]
265+
[Alias('Identify','Handshake')]
261266
[PSObject]
262-
$Reply,
267+
$Handshake,
263268

264269
# If set, will watch the output of the WebSocket job, outputting results continuously instead of outputting a websocket job.
265270
[Parameter(ParameterSetName='WebSocketClient')]
@@ -430,6 +435,7 @@ function Get-WebSocket {
430435
$SkipCount = [long]0
431436

432437
$saidHello = $null
438+
$shookHands = $null
433439

434440
:WebSocketMessageLoop while ($true) {
435441
if ($ws.State -ne 'Open') {
@@ -477,6 +483,24 @@ function Get-WebSocket {
477483
$MessageCount++
478484

479485
try {
486+
if ($Handshake -and -not $shookHands) {
487+
# a number of websockets require some handshaking
488+
$handShakeMessage =
489+
if ($Handshake -is [ScriptBlock]) {
490+
& $Handshake $MessageObject
491+
} else {
492+
$Handshake
493+
}
494+
495+
if ($handShakeMessage) {
496+
if ($handShakeMessage -isnot [string]) {
497+
$saidHello = $ws.SendAsync([ArraySegment[byte]]::new(
498+
$OutputEncoding.GetBytes((ConvertTo-Json -InputObject $handShakeMessage -Depth 10))
499+
), 'Text', $true, $CT)
500+
}
501+
}
502+
}
503+
480504
$webSocketMessage =
481505
if ($Binary) {
482506
$Buf -gt 0

0 commit comments

Comments
 (0)