Advertisement
Yevrag35

ServerSentEvents-Client

Apr 26th, 2020
1,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param
  3. (
  4.     [Parameter(Mandatory=$true, Position=0)]
  5.     [uri] $Url,
  6.  
  7.     [Parameter(Mandatory=$true, Position=1)]
  8.     [string]$BearerToken
  9. )
  10.  
  11. $messageQueue = New-Object -TypeName "System.Collections.Generic.List[string]" -ArgumentList 1024
  12. $receivedObjects = New-Object -TypeName "System.Collections.Generic.List[object]" -ArgumentList 100
  13.  
  14. Function Process-Lines([ref]$Queue, [ref]$List)
  15. {
  16.     $index = 0
  17.     $lastEventIndex = -1
  18.     for ($i = 0; $i -lt $Queue.Value.Count; $i++)
  19.     {
  20.         $line = $Queue.Value[$i]
  21.         if ([string]::IsNullOrWhiteSpace($line))
  22.         {
  23.             continue
  24.         }
  25.         $line = $line.Trim()
  26.  
  27.         if ($line -like "event:*")
  28.         {
  29.             $lastEvent = [pscustomobject]@{
  30.                 Name = $line -replace "event:", ""
  31.                 Data = $null
  32.             }
  33.            
  34.         }
  35.         elseif ($line -like "data:*")
  36.         {
  37.             if ($null -eq $lastEvent)
  38.             {
  39.                 continue  
  40.             }
  41.  
  42.             $lastEvent.Data = $line -replace 'data:', ''
  43.             $List.Value.Add($lastEvent)
  44.  
  45.             Write-Host "`nFound event: $index"
  46.             Write-Host "Name: $($lastEvent.Name)"
  47.             Write-Host "Data: $($lastEvent.Data)"
  48.             $index++
  49.             $lastEventIndex = $i
  50.         }
  51.     }
  52.  
  53.     if ($lastEventIndex -ge 0)
  54.     {
  55.         $Queue.Value.RemoveRange(0, $lastEventIndex)
  56.     }
  57. }
  58.  
  59. Function Push-Text([string]$text, [ref]$Queue, [ref]$List)
  60. {
  61.     if ([string]::IsNullOrWhiteSpace($text))
  62.     {
  63.         return
  64.     }
  65.  
  66.     $lines = $text.Trim() -split "`n"
  67.     $Queue.Value.AddRange($lines)
  68.  
  69.     if ($text -like "*data:*")
  70.     {
  71.         Process-Lines -Queue $Queue -List $List
  72.     }
  73. }
  74.  
  75. Function Read-StreamForever([System.IO.Stream]$Stream, [ref]$Queue, [ref]$List)
  76. {
  77.     $encoding = [System.Text.Encoding]::UTF8
  78.     [byte[]]$buffer = New-Object byte[] 2048
  79.     while ($true)
  80.     {
  81.         if ($Stream.CanRead)
  82.         {
  83.             $length = $Stream.Read($buffer, 0, 2048)
  84.             if ($length -gt 0)
  85.             {
  86.                 $text = $encoding.GetString($buffer, 0, $length)
  87.                 Push-Text -Text $text -Queue $Queue -List $List
  88.             }
  89.         }
  90.     }
  91. }
  92.  
  93. Function Open-SSEStream([uri]$Url, [string]$token, [ref]$Queue, [ref]$List)
  94. {
  95.     $request = [System.Net.WebRequest]::CreateHttp($Url)
  96.     $request.Headers.Add("Authorization", "Bearer $token")
  97.     $request.AllowReadStreamBuffering = $false
  98.     $response = $request.GetResponse()
  99.     $stream = $response.GetResponseStream()
  100.  
  101.     Read-StreamForever -Queue $Queue -Stream $stream -List $List
  102.  
  103.     return $stream
  104. }
  105.  
  106. Write-Host "Attempting to open stream"
  107. $response = Open-SSEStream -Url $Url -Token $BearerToken -Queue ([ref]$messageQueue) -List ([ref]$receivedObjects)
  108. if ($?)
  109. {
  110.     Write-Host "`nSuccess!"
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement