-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGet-WebSocket.json
More file actions
98 lines (98 loc) · 6.13 KB
/
Get-WebSocket.json
File metadata and controls
98 lines (98 loc) · 6.13 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
{
"Synopsis": "WebSockets in PowerShell.",
"Description": "Get-WebSocket gets a websocket.\n\nThis will create a job that connects to a WebSocket and outputs the results.\n\nIf the `-Watch` parameter is provided, will output a continous stream of objects.",
"Parameters": [
{
"Name": null,
"Type": null,
"Description": "",
"Required": false,
"Position": 0,
"Aliases": null,
"DefaultValue": null,
"Globbing": false,
"PipelineInput": null,
"variableLength": false
}
],
"Notes": [
null
],
"CommandType": "Function",
"Component": [
null
],
"Inputs": [
null
],
"Outputs": [
null
],
"Links": [
"https://websocket.powershellweb.com/Get-WebSocket/",
"https://learn.microsoft.com/en-us/dotnet/api/system.net.websockets.clientwebsocket?wt.mc_id=MVP_321542",
"https://learn.microsoft.com/en-us/dotnet/api/system.net.httplistener?wt.mc_id=MVP_321542"
],
"Examples": [
{
"Title": "EXAMPLE 1",
"Markdown": "Create a WebSocket job that connects to a WebSocket and outputs the results.",
"Code": "$socketServer = Get-WebSocket -RootUrl \"http://localhost:8387/\" -HTML \"<h1>WebSocket Server</h1>\"\n$socketClient = Get-WebSocket -SocketUrl \"ws://localhost:8387/\"\nforeach ($n in 1..10) { $socketServer.Send(@{n=Get-Random}) }\n$socketClient | Receive-Job -Keep"
},
{
"Title": "EXAMPLE 2",
"Markdown": "Get is the default verb, so we can just say WebSocket.\n`-Watch` will output a continous stream of objects from the websocket.\nFor example, let's Watch BlueSky, but just the text ",
"Code": "websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Watch -Maximum 1kb |\n % { \n $_.commit.record.text\n }"
},
{
"Title": "EXAMPLE 3",
"Markdown": "Watch BlueSky, but just the text and spacing",
"Code": "$blueSkySocketUrl = \"wss://jetstream2.us-$(\n 'east','west'|Get-Random\n).bsky.network/subscribe?$(@(\n \"wantedCollections=app.bsky.feed.post\"\n) -join '&')\"\nwebsocket $blueSkySocketUrl -Watch | \n % { Write-Host \"$(' ' * (Get-Random -Max 10))$($_.commit.record.text)$($(' ' * (Get-Random -Max 10)))\"} -Max 1kb"
},
{
"Title": "EXAMPLE 4",
"Markdown": "Watch continuously in a background job.",
"Code": "websocket wss://jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post"
},
{
"Title": "EXAMPLE 5",
"Markdown": "Watch the first message in -Debug mode. \nThis allows you to literally debug the WebSocket messages as they are encountered.",
"Code": "websocket wss://jetstream2.us-west.bsky.network/subscribe -QueryParameter @{\n wantedCollections = 'app.bsky.feed.post'\n} -Max 1 -Debug"
},
{
"Title": "EXAMPLE 6",
"Markdown": "Watch BlueSky, but just the emoji",
"Code": "websocket jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Tail -Max 1kb |\n Foreach-Object {\n $in = $_\n if ($in.commit.record.text -match '[\\p{IsHighSurrogates}\\p{IsLowSurrogates}]+') {\n Write-Host $matches.0 -NoNewline\n }\n }"
},
{
"Title": "EXAMPLE 7",
"Markdown": "",
"Code": "$emojiPattern = '[\\p{IsHighSurrogates}\\p{IsLowSurrogates}\\p{IsVariationSelectors}\\p{IsCombiningHalfMarks}]+)'\nwebsocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Tail |\n Foreach-Object {\n $in = $_\n $spacing = (' ' * (Get-Random -Minimum 0 -Maximum 7))\n if ($in.commit.record.text -match \"(?>(?:$emojiPattern|\\#\\w+)\") {\n $match = $matches.0 \n Write-Host $spacing,$match,$spacing -NoNewline\n }\n }"
},
{
"Title": "EXAMPLE 8",
"Markdown": "",
"Code": "websocket wss://jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Watch |\n Where-Object {\n $_.commit.record.embed.'$type' -eq 'app.bsky.embed.external'\n } |\n Foreach-Object {\n $_.commit.record.embed.external.uri\n }"
},
{
"Title": "EXAMPLE 9",
"Markdown": "BlueSky, but just the hashtags",
"Code": "websocket wss://jetstream2.us-west.bsky.network/subscribe -QueryParameter @{\n wantedCollections = 'app.bsky.feed.post'\n} -WatchFor @{\n {$webSocketoutput.commit.record.text -match \"\\#\\w+\"}={\n $matches.0\n } \n} -Maximum 1kb"
},
{
"Title": "EXAMPLE 10",
"Markdown": "BlueSky, but just the hashtags (as links)",
"Code": "websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -WatchFor @{\n {$webSocketoutput.commit.record.text -match \"\\#\\w+\"}={\n if ($psStyle.FormatHyperlink) {\n $psStyle.FormatHyperlink($matches.0, \"https://bsky.app/search?q=$([Web.HttpUtility]::UrlEncode($matches.0))\")\n } else {\n $matches.0\n }\n }\n}"
},
{
"Title": "EXAMPLE 11",
"Markdown": "",
"Code": "websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -WatchFor @{\n {$args.commit.record.text -match \"\\#\\w+\"}={\n $matches.0\n }\n {$args.commit.record.text -match '[\\p{IsHighSurrogates}\\p{IsLowSurrogates}]+'}={\n $matches.0\n }\n}"
},
{
"Title": "EXAMPLE 12",
"Markdown": "We can decorate a type returned from a WebSocket, allowing us to add additional properties.\nFor example, let's add a `Tags` property to the `app.bsky.feed.post` type.",
"Code": "$typeName = 'app.bsky.feed.post'\nUpdate-TypeData -TypeName $typeName -MemberName 'Tags' -MemberType ScriptProperty -Value {\n @($this.commit.record.facets.features.tag)\n} -Force\n\n# Now, let's get 10kb posts ( this should not take too long )\n$somePosts =\n websocket \"wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=$typeName\" -PSTypeName $typeName -Maximum 10kb -Watch\n$somePosts |\n ? Tags |\n Select -ExpandProperty Tags |\n Group |\n Sort Count -Descending |\n Select -First 10"
}
]
}