forked from Badgerati/Pode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeb-SignalConnection.ps1
More file actions
81 lines (65 loc) · 2.72 KB
/
Web-SignalConnection.ps1
File metadata and controls
81 lines (65 loc) · 2.72 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
<#
.SYNOPSIS
Sample script to set up a Pode server with WebSocket support, listening on localhost.
.DESCRIPTION
This script initializes a Pode server that listens on localhost:8081 with WebSocket support.
It includes logging to the terminal and several routes and timers for WebSocket connections.
The server can connect to a WebSocket from an external script and respond to messages.
.PARAMETER ScriptPath
Path of the script being executed.
.PARAMETER podePath
Path of the Pode module.
.EXAMPLE
To run the sample: ./Web-SignalConnection.ps1
Run this script to start the Pode server and navigate to 'http://localhost:8081' in your browser.
The server supports WebSocket connections and includes routes for connecting and resetting
WebSocket connections.
Invoke-RestMethod -Uri http://localhost:8081/connect/ -Method Get
Invoke-RestMethod -Uri http://localhost:8081/reset/ -Method Get
.LINK
https://github.com/Badgerati/Pode/blob/develop/examples/Web-SignalConnection.ps1
.NOTES
Author: Pode Team
License: MIT License
#>
try {
# Determine the script path and Pode module path
$ScriptPath = (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
$podePath = Split-Path -Parent -Path $ScriptPath
# Import the Pode module from the source path if it exists, otherwise from installed modules
if (Test-Path -Path "$($podePath)/src/Pode.psm1" -PathType Leaf) {
Import-Module "$($podePath)/src/Pode.psm1" -Force -ErrorAction Stop
}
else {
Import-Module -Name 'Pode' -MaximumVersion 2.99 -ErrorAction Stop
}
}
catch { throw }
# or just:
# Import-Module Pode
# create a server, and start listening
Start-PodeServer -EnablePool WebSockets {
# listen
Add-PodeEndpoint -Address localhost -Port 8081 -Protocol Http
# log requests to the terminal
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging -Level Error, Debug, Verbose
# connect to web socket from web-signal.ps1
Connect-PodeWebSocket -Name 'Example' -Url 'ws://localhost:8091' -ScriptBlock {
$WsEvent.Data | Out-Default
if ($WsEvent.Data.message -inotlike '*Ex:*') {
Send-PodeWebSocket -Message @{ message = "Ex: $($WsEvent.Data.message)" }
}
}
Add-PodeRoute -Method Get -Path '/connect' -ScriptBlock {
Connect-PodeWebSocket -Name 'Test' -Url 'wss://ws.ifelse.io/' -ScriptBlock {
$WsEvent.Request | out-default
}
}
Add-PodeTimer -Name 'Test' -Interval 10 -ScriptBlock {
$rand = Get-Random -Minimum 10 -Maximum 1000
Send-PodeWebSocket -Name 'Test' -Message "hello $rand"
}
Add-PodeRoute -Method Get -Path '/reset' -ScriptBlock {
Reset-PodeWebSocket -Name 'Example'
}
}