-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPMessage4OBS_server.ps1
76 lines (62 loc) · 1.75 KB
/
TCPMessage4OBS_server.ps1
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
#############
#
# Variables
#
#############
$Global:OBSCommand = "$($PSScriptRoot)\..\OBSCommand\OBSCommand.exe"
#############
#
# Functions
#
#############
Function Receive-TCPMessage {
Param (
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[int] $Port
)
Process {
Try {
# Set up endpoint and start listening
$endpoint = new-object System.Net.IPEndPoint([ipaddress]::any,$port)
$listener = new-object System.Net.Sockets.TcpListener $EndPoint
$listener.start()
# Wait for an incoming connection
$data = $listener.AcceptTcpClient()
# Stream setup
$stream = $data.GetStream()
$bytes = New-Object System.Byte[] 1024
# Read data from stream and write it to host
while (($i = $stream.Read($bytes,0,$bytes.Length)) -ne 0){
$EncodedText = New-Object System.Text.ASCIIEncoding
$data = $EncodedText.GetString($bytes,0, $i)
if ($data -match '=.+' -and $data -notlike "*ok*"){
Invoke-OBSCommand $data
}
}
# Close TCP connection and stop listening
$stream.close()
$listener.stop()
}
Catch {
"Receive Message failed with: `n" + $Error[0]
}
}
}
function Invoke-OBSCommand {
param (
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[string]$command
)
$command = $command.Replace("`r`n",'')
& $Global:OBSCommand "/command$($command)"
}
#############
#
# Script
#
#############
while($true){
Receive-TCPMessage -Port 29800
}