Hello,
You might find this code useful if you need to develop some functionality outside EG with C#, but still need to have you C# prog communicate with EG.
The provided code implements a network sender and a network receiver in C#. It is ready to be included in any C# Code.
It speaks threw the network with the network event receiver plugin provided with eg, and also with the network event sender plugin also provided with eg (see the plugins list)
Here is how to use it :
You will find an EG_Network_Event_Receiver_Sender class. This class implements both the server and the client. It means that you can send event to eg with it and also that eg can send event to your C# App.
When you create a new EG_Network_Event_Receiver_Sender object, the server starts automatically :
- Code: Select all
EG_Network_Event_Receiver_Sender eg = new EG_Network_Event_Receiver_Sender("*:25632", "password", false);
The parameters are :
"localIp:Port" : localIp is the Ip Adress on which the server will listen. if you put *, it will listen on any interface of your computer.
"password" : password is the password needed to connect to your server
false : specify if your server supports enduring events or not (the network event sender/receiver of eg always generate enduring events, but in a manner that is useless. It's up to you to decide if you need it or not).
Once the server is started, if a client sends him an eg event, it will fire an event (I mean a C# event). To have your code react to this event, you need to subscribe to it.
This is done by writting this :
- Code: Select all
eg.Event_FromNetworkEventSender += new EG_Network_Event_Receiver_Sender.Event_FromNetworkEventSender_Handler(eg_Event_FromNetworkEventSender);
When you write this, you tell C# that when the server receives an event from an eg client, it shoud run eg_Event_FromNetworkEventSender
So you need to write an eg_Event_FromNetworkEventSender method :
- Code: Select all
static void eg_Event_FromNetworkEventSender(object sender, NetWorkEventReceiver_EventArgs e)
{
Console.WriteLine("Event : " + e.name);
foreach (string k in e.payload)
{
Console.WriteLine(" payload = " + k);
}
Console.WriteLine("");
}
In this example, the event is just printed to the console, but you might code whatever you need here. It's up to you to decide. You get the name of the event in e.name and the payload in e.payload.
The EG_Network_Event_Receiver_Sender also implements a client.
Once you have created you EG_Network_Event_Receiver_Sender object, you can just call :
- Code: Select all
eg.SendEventToNetworkEventReceiver("127.0.0.1:25632", "password", "testEvent", new string[3] { "1", "2", "12" })
This will actually send testEvent to the server that listen on 127.0.0.1:25632. The last param is the payload which will be send with the event. It must be a string array.
If all went fine, this method will return "success". Otherwise, it will return a string that indicates where things gets wrong :
"Error while trying to receive the \"accept\"" which might reveal a password problem.
If you directly run my code without modifying it, it will :
Create the server :
- Code: Select all
EG_Network_Event_Receiver_Sender eg = new EG_Network_Event_Receiver_Sender("*:25632", "password", false);
Then subscribe to the events that the server might fire :
- Code: Select all
eg.EnduringEvent_FromNetworkEventSender_Start += new EG_Network_Event_Receiver_Sender.EnduringEvent_FromNetworkEventSender_Start_Handler(eg_EnduringEvent_FromNetworkEventSender_Start);
eg.EnduringEvent_FromNetworkEventSender_End += new EG_Network_Event_Receiver_Sender.EnduringEvent_FromNetworkEventSender_End_Handler(eg_EnduringEvent_FromNetworkEventSender_End);
eg.Event_FromNetworkEventSender += new EG_Network_Event_Receiver_Sender.Event_FromNetworkEventSender_Handler(eg_Event_FromNetworkEventSender);
Then wait a few miliseconds to let the server start
- Code: Select all
System.Threading.Thread.Sleep(500);
Then send and event to the server which has just started and report any error :
- Code: Select all
if ("success" != (status = eg.SendEventToNetworkEventReceiver("127.0.0.1:25632", "password", "testEvent", new string[3] { "1", "2", "12" })))
{
Console.WriteLine(status);
}
Since we created a server that doesn't support enduring events (false parameter) the server will only fire Event_FromNetworkEventSender. When this event is fired, we told the server to run
- Code: Select all
static void eg_Event_FromNetworkEventSender(object sender, NetWorkEventReceiver_EventArgs e)
{
Console.WriteLine("Event : " + e.name);
foreach (string k in e.payload)
{
Console.WriteLine(" payload = " + k);
}
Console.WriteLine("");
}
So the event that we have just sent is simply printed to the console.
The demo is over, so the server is stopped properly before the program terminates :
- Code: Select all
eg.StopLocalNetworkEventReceiver();
I hope these short explanations will help you. Let me know if you need more informations.
Regards,