@@ -56,6 +56,97 @@ if ($message -is [Collections.IList] -and $message -isnot [byte[]]) {
56
56
57
57
58
58
59
+ </Script >
60
+ </ScriptMethod >
61
+ </Members >
62
+ </Type >
63
+ <Type >
64
+ <Name >WebSocket.Server.ThreadJob</Name >
65
+ <Members >
66
+ <ScriptMethod >
67
+ <Name >Pop</Name >
68
+ <Script >
69
+ param()
70
+
71
+ if ($this.Output.Count -gt 0) {
72
+ $this.Output[0]
73
+ $this.Output.RemoveAt(0)
74
+ }
75
+ </Script >
76
+ </ScriptMethod >
77
+ <ScriptMethod >
78
+ <Name >Send</Name >
79
+ <Script >
80
+ < #
81
+ .SYNOPSIS
82
+ Sends a WebSocket message.
83
+ .DESCRIPTION
84
+ Sends a message from a WebSocket server.
85
+ #>
86
+ param(
87
+ [PSObject]
88
+ $Message,
89
+
90
+ [string]
91
+ $Pattern
92
+ )
93
+
94
+ function sendMessage {
95
+ param([Parameter(ValueFromPipeline)]$msg, [PSObject[]]$Sockets)
96
+ process {
97
+ if ($msg -is [byte[]]) {
98
+ $messageSegment = [ArraySegment[byte]]::new($msg)
99
+ foreach ($socket in $sockets) {
100
+ if ($null -ne $messageSegment -and $socket.SendAsync) {
101
+ $null = $socket.SendAsync($messageSegment, 'Binary', 'EndOfMessage',[Threading.Cancellationtoken]::None)
102
+ }
103
+ }
104
+
105
+ } else {
106
+ $jsonMessage = ConvertTo-Json -InputObject $msg
107
+ $messageSegment = [ArraySegment[byte]]::new($OutputEncoding.GetBytes($jsonMessage))
108
+ foreach ($socket in $sockets) {
109
+ if ($null -ne $messageSegment -and $socket.SendAsync) {
110
+ $null = $socket.SendAsync($messageSegment, 'Binary', 'EndOfMessage',[Threading.Cancellationtoken]::None)
111
+ }
112
+ }
113
+ }
114
+ $msg
115
+ }
116
+ }
117
+
118
+ $patternAsRegex = $pattern -as [regex]
119
+ $socketList = @(
120
+ foreach ($socketConnection in $this.HttpListener.SocketRequests.Values) {
121
+ if (
122
+ $patternAsRegex -and
123
+ $socketConnection.WebSocketContext.RequestUri -match $pattern
124
+ ) {
125
+ $socketConnection.WebSocket
126
+ }
127
+ elseif (
128
+ $pattern -and
129
+ $socketConnection.WebSocketContext.RequestUri -like $pattern
130
+ ) {
131
+ $socketConnection.WebSocket
132
+ }
133
+ else {
134
+ $socketConnection.WebSocket
135
+ }
136
+ }
137
+ )
138
+
139
+
140
+ if ($message -is [Collections.IList] -and $message -isnot [byte[]]) {
141
+ $Message | sendMessage -Sockets $socketList
142
+ } else {
143
+ sendMessage -msg $Message -Sockets $socketList
144
+ }
145
+
146
+
147
+
148
+
149
+
59
150
</Script >
60
151
</ScriptMethod >
61
152
</Members >
0 commit comments