Skip to content

Commit a34e862

Browse files
author
James Brundage
committed
feat: Get-WebSocket -RawText ( Fixes #30 )
1 parent 5758738 commit a34e862

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ function Get-WebSocket {
137137
[switch]
138138
$Watch,
139139

140+
# If set, will output the raw text of the WebSocket job.
141+
[Alias('Raw')]
142+
[switch]
143+
$RawText,
144+
140145
# If set, will watch the output of a WebSocket job for one or more conditions.
141146
# The conditions are the keys of the dictionary, and can be a regex, a string, or a scriptblock.
142147
# The values of the dictionary are what will happen when a match is found.
@@ -230,15 +235,20 @@ function Get-WebSocket {
230235
break
231236
}
232237

233-
234238
$Buf = [byte[]]::new($BufferSize)
235239
$Seg = [ArraySegment[byte]]::new($Buf)
236240
$null = $ws.ReceiveAsync($Seg, $CT).Wait()
237241
$MessageCount++
238-
$JS = $OutputEncoding.GetString($Buf, 0, $Buf.Count)
239-
if ([string]::IsNullOrWhitespace($JS)) { continue }
240-
try {
241-
$webSocketMessage = ConvertFrom-Json $JS
242+
243+
try {
244+
$webSocketMessage =
245+
if ($Raw) {
246+
$OutputEncoding.GetString($Buf, 0, $Buf.Count)
247+
} else {
248+
$JS = $OutputEncoding.GetString($Buf, 0, $Buf.Count)
249+
if ([string]::IsNullOrWhitespace($JS)) { continue }
250+
ConvertFrom-Json $JS
251+
}
242252
if ($handler) {
243253
$psCmd =
244254
if ($runspace.LanguageMode -eq 'NoLanguage' -or
@@ -318,7 +328,7 @@ function Get-WebSocket {
318328
}
319329
$webSocketJob.pstypenames.insert(0, 'WebSocketJob')
320330
if ($Watch) {
321-
do {
331+
do {
322332
$webSocketJob | Receive-Job
323333
Start-Sleep -Milliseconds (
324334
7, 11, 13, 17, 19, 23 | Get-Random

0 commit comments

Comments
 (0)