Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Created March 20, 2015 06:43
Show Gist options
  • Save Jaykul/d0ee7603a6dff6ffecac to your computer and use it in GitHub Desktop.
Save Jaykul/d0ee7603a6dff6ffecac to your computer and use it in GitHub Desktop.
PowerShell NetMQ
# Set up the proxy forwarder ...
Add-Type -path .\NetMQ\lib\net40\NetMQ.dll
Start-Job {
try {
$context = [NetMQ.NetMQContext]::Create()
$publisher = $context.CreateXPublisherSocket()
$subscriber = $context.CreateXSubscriberSocket()
$publisher.Bind("tcp://127.0.0.1:50007");
$subscriber.Bind("tcp://127.0.0.1:50014");
$proxy = [NetMQ.Proxy]::new($subscriber, $publisher, $null)
$proxy.Start();
} finally {
$context.Dispose()
$publisher.Dispose()
$subscriber.Dispose()
}
}
# Set up a Publisher
Add-Type -path .\NetMQ\lib\net40\NetMQ.dll
try {
$context = [NetMQ.NetMQContext]::Create()
$publisher = $context.CreatePublisherSocket()
$publisher.Connect("tcp://127.0.0.1:50014");
function Send-Message {
param($Message, $Channel = 1)
[NetMQ.OutgoingSocketExtensions]::Send( $publisher, "CHANNEL$Channel", $false, $true )
[NetMQ.OutgoingSocketExtensions]::Send( $publisher, $Message )
}
Send-Message "Hello World"
} finally {
$context.Dispose()
$publisher.Dispose()
}
# Set up a listener
Add-Type -path .\NetMQ\lib\net40\NetMQ.dll
try {
$context = [NetMQ.NetMQContext]::Create()
$subscriber = $context.CreateSubscriberSocket()
$subscriber.Connect("tcp://127.0.0.1:50007");
$publisher.Connect("tcp://127.0.0.1:50014");
$subscriber.Subscribe("CHANNEL1")
$subscriber.Subscribe("CHANNEL2")
while ($true)
{
# This is always going to pull either "IRC" or "BOT" ...
$Channel = [NetMQ.ReceivingSocketExtensions]::ReceiveString($subscriber)
$message = [NetMQ.ReceivingSocketExtensions]::ReceiveString($subscriber)
Write-Host "[$Channel]: $message"
}
} finally {
$context.Dispose()
$subscriber.Dispose()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment