@@ -256,10 +256,15 @@ function Get-WebSocket {
256
256
[Alias (' Authorize' , ' HelloMessage' )]
257
257
[PSObject ]
258
258
$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' )]
261
266
[PSObject ]
262
- $Reply ,
267
+ $Handshake ,
263
268
264
269
# If set, will watch the output of the WebSocket job, outputting results continuously instead of outputting a websocket job.
265
270
[Parameter (ParameterSetName = ' WebSocketClient' )]
@@ -430,6 +435,7 @@ function Get-WebSocket {
430
435
$SkipCount = [long ]0
431
436
432
437
$saidHello = $null
438
+ $shookHands = $null
433
439
434
440
:WebSocketMessageLoop while ($true ) {
435
441
if ($ws.State -ne ' Open' ) {
@@ -477,6 +483,24 @@ function Get-WebSocket {
477
483
$MessageCount ++
478
484
479
485
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
+
480
504
$webSocketMessage =
481
505
if ($Binary ) {
482
506
$Buf -gt 0
0 commit comments