Skip to content

Commit

Permalink
#895: separate out remove and disconnect functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed May 12, 2022
1 parent 7d2f20f commit 4a9573d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/Listener/PodeReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ public void DisconnectWebSocket(string name)
return;
}

WebSockets[name].Dispose();
}
}

public void RemoveWebSocket(string name)
{
lock (WebSockets)
{
if (!WebSockets.ContainsKey(name))
{
return;
}

WebSockets[name].Dispose();
WebSockets.Remove(name);
}
Expand Down
1 change: 1 addition & 0 deletions src/Pode.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
'Set-PodeWebSocketConcurrency',
'Connect-PodeWebSocket',
'Disconnect-PodeWebSocket',
'Remove-PodeWebSocket',
'Send-PodeWebSocket',
'Reset-PodeWebSocket',
'Test-PodeWebSocket'
Expand Down
38 changes: 32 additions & 6 deletions src/Public/WebSockets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,28 @@ function Disconnect-PodeWebSocket

if (Test-PodeWebSocket -Name $Name) {
$PodeContext.Server.WebSockets.Receiver.DisconnectWebSocket($Name)
$PodeContext.Server.WebSockets.Connections.Remove($Name)
}
}

function Remove-PodeWebSocket
{
[CmdletBinding()]
param(
[Parameter()]
[string]
$Name
)

if ([string]::IsNullOrWhiteSpace($Name) -and ($null -ne $WsEvent)) {
$Name = $WsEvent.Request.WebSocket.Name
}

if ([string]::IsNullOrWhiteSpace($Name)) {
throw "No Name for a WebSocket to remove supplied"
}

#TODO: this should just disconnect, not disconnect and remove
# make a new Remove-WS to disconnect and remove
$PodeContext.Server.WebSockets.Receiver.RemoveWebSocket($Name)
$PodeContext.Server.WebSockets.Connections.Remove($Name)
}

function Send-PodeWebSocket
Expand Down Expand Up @@ -150,7 +167,7 @@ function Send-PodeWebSocket
}

if (Test-PodeWebSocket -Name $Name) {
$ws.Send($Message, $Type)
$PodeContext.Server.WebSockets.Receiver.GetWebSocket($Name).Send($Message, $Type)
}
}

Expand All @@ -177,7 +194,7 @@ function Reset-PodeWebSocket
}

if (Test-PodeWebSocket -Name $Name) {
$ws.Reconnect($Url)
$PodeContext.Server.WebSockets.Receiver.GetWebSocket($Name).Reconnect($Url)
}
}

Expand All @@ -190,5 +207,14 @@ function Test-PodeWebSocket
$Name
)

return ($null -ne $PodeContext.Server.WebSockets.Receiver.GetWebSocket($Name))
$found = ($null -ne $PodeContext.Server.WebSockets.Receiver.GetWebSocket($Name))
if ($found) {
return $true
}

if ($PodeContext.Server.WebSockets.Connections.ContainsKey($Name)) {
Remove-PodeWebSocket -Name $Name
}

return $false
}

0 comments on commit 4a9573d

Please sign in to comment.