Option 2: NuGet Package Manager for Solution Interface
Open the NuGet Package Manager for Solution Interface: Tools -> NuGet Package Manager -> Manage NuGet Package for Solution and browse Nep. Select the projects where the NEP library for C# will be installed and select the install button. In the same way, install the Newtonsoft.Jsonand NetMQpackages
Test a basic Publisher
using Nep;
namespace NepTest
{
// Message to send, used for serialization in JSON
class Msg
{
public int message { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Create a new nep node
// The parameter given to this function must be different for each script
NepNode node = new NepNode("basic_pub_cs");
// Create a new publisher with the topic "test"
NepPublisher pub = node.NewPub("test", "json");
// Define the message to send
Msg msg = new Msg();
int i = 0;
while (i < 50)
{
i++;
// Fill message to send
msg.message = i;
// Send the message with the next line
pub.Publish(msg);
// Use the next line to send messages each 0.5 seconds approx.
System.Threading.Thread.Sleep(500);
}
// Close sockets
pub.Dispose();
node.Close();
Console.WriteLine("Publisher program finished!");
}
}
}
Test a basic Subscriber
uusing System;
using Nep;
using Newtonsoft.Json;
// Change <NepTest> for the correct namespace
namespace NepTest
{
// Message to read, used for deserialization
class Msg
{
// JSON key = message, value is an int
public int message { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Create a new Nep Node with a specified name
NepNode node = new NepNode("basic_sub_cs");
// Create a new Subscriber for the "test" topic with "json" message type
NepSubscriber sub = node.NewSub("test", "json");
String msg = "";
// Print the received messages
int i = 0;
while (i < 50)
{
// Non-blocking listen to receive a message
bool ok = sub.Listen(out msg);
if (ok)
{
// Convert JSON string to Msg object using JsonConvert
Msg message = JsonConvert.DeserializeObject<Msg>(msg);
Console.WriteLine(message.message);
i = i + 1;
}
}
// Close the Subscriber and Node
sub.Dispose();
node.Close();
Console.WriteLine("Subscriber program finished!");
}
}
}