Repository navigation
Expand file tree
/
Copy pathStart-ModuleWebSocketHandler.ps1
More file actions
124 lines (109 loc) · 5.33 KB
/
Copy pathStart-ModuleWebSocketHandler.ps1
File metadata and controls
124 lines (109 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<#
.SYNOPSIS
Manage a persistent WebSocket connection and spawn worker threads.
.DESCRIPTION
`Start-ModuleWebSocketHandler` drives the lifecycle for a named WebSocket
connection. It attempts to establish the connection, performs any type-
specific authentication steps (for example MyWebSocket), and
spawns reader, writer and processing worker threads. The handler runs in a
loop and is responsible for reconnection and keep-alive handling.
.PARAMETER Name
| Name | Type | Description | Default |
|------|---------------|-----------------------------------------------|---------|
| Name | System.String | Name of the WebSocket configuration to manage | |
.OUTPUTS
System.Object -- Emits structured return codes for validation failures; otherwise
the function runs until the thread manager requests termination.
.EXAMPLE
PS> Start-ModuleWebSocketHandler -Name 'MySocket'
.NOTES
The function expects a corresponding configuration entry in
`$Global:ModuleName.Public.WebSocket.Config` for the provided `Name`. It runs
inside a dedicated thread spawned by `New-ModuleWebSocketEnvironment` and drives
connection, reconnection, keep-alive handling, as well as spawning of reader,
writer and processing worker threads.
SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com>
SPDX-License-Identifier: GPL-3.0-or-later
#>
function Start-ModuleWebSocketHandler()
{
param (
[string]$Name = ''
);
# We only require the name, as we can fetch the rest from the global config
if ([string]::IsNullOrEmpty($Name)) {
return (Write-FunctionReturnValue -ReturnValue 'WebSocketHandlerNoNameProvided');
};
$GlobalVariable = Get-GlobalRuntimeVariable;
[string]$WebSocketAddress = $GlobalVariable.Public.WebSocket.Config.$Name.Address;
[string]$WebSocketType = $GlobalVariable.Public.WebSocket.Config.$Name.Type;
# Only test for the thread itself here, as WebSockets will be handled inside the loop
# We need to keep this thread active, as it will handle the re-connections and keep-alive events
# While it will start read and write threads as needed
while (Test-ThreadRunning) {
# If the WebSocket is already connected, we can just continue
# TODO: We should make this dynamic with a configuration instead of hardcoding
if (Test-ModuleWebSocket) {
switch ($WebSocketType) {
<#'ExampleWebSocketType' {
Invoke-ExampleWebSocket -HandlerEvents | Out-Null;
break;
};#>
default {
break;
}
}
continue;
}
# Try to connect the WebSocket
Connect-ModuleWebSocket -Name $Name -Address $WebSocketAddress | Out-Null;
# TODO: We should make this dynamic with a configuration instead of hardcoding
switch ($WebSocketType) {
<#'ExampleWebSocketType' {
Invoke-ExampleWebSocket -HandlerEvents | Out-Null;
break;
};#>
default {
Write-LoggingEntry -Message 'Start-ModuleWebSocketHandler: Unknown WebSocket type, cannot send authentication message' -Level 'Warning' -Data @{ 'WebSocketType' = $WebSocketType; 'Name' = $Name; };
break;
};
}
# Start the background read and write threads only if the connection was successful
if (Test-ModuleWebSocket) {
$GlobalVariable.Public.WebSocket.$Name.WriterWorker.Add('WebSocket::CoreTestWriteObject') | Out-Null;
$GlobalVariable.Public.WebSocket.$Name.ReaderWorker.Add('WebSocket::CoreTestWriteObject') | Out-Null;
# Now we need to start a thread for reading the WebSocket messages and writing into the socket
# Read messages from the WebSocket
New-Thread `
-Command 'Read-ModuleWebSocket' `
-Parameters @{
'Name' = $Name;
'ClientWebSocket' = $GlobalVariable.Private.WebSocket.ClientWebSocket;
'CancellationTokenSource' = $GlobalVariable.Private.WebSocket.CancellationTokenSource;
} `
-Start `
-WaitForJobs | Out-Null;
# Write messages to the WebSocket
New-Thread `
-Command 'Write-ModuleWebSocket' `
-Parameters @{
'Name' = $Name;
'ClientWebSocket' = $GlobalVariable.Private.WebSocket.ClientWebSocket;
'CancellationTokenSource' = $GlobalVariable.Private.WebSocket.CancellationTokenSource;
} `
-Start `
-WaitForJobs | Out-Null;
# Start the worker which will actually handle the messages read from the WebSocket
New-Thread `
-Command 'Start-ModuleWebSocketReaderWorker' `
-Parameters @{
'Name' = $Name;
'ClientWebSocket' = $GlobalVariable.Private.WebSocket.ClientWebSocket;
'CancellationTokenSource' = $GlobalVariable.Private.WebSocket.CancellationTokenSource;
} `
-Start `
-WaitForJobs | Out-Null;
}
}
Disconnect-ModuleWebSocket;
}