forked from haneytron/simplsockets
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServerSendMessage.cs
More file actions
61 lines (49 loc) · 1.93 KB
/
ServerSendMessage.cs
File metadata and controls
61 lines (49 loc) · 1.93 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
using System;
using SimplMessage;
using System.Net;
namespace SimplSocketsClient
{
public class ServerSendMessage
{
public void Start()
{
CreateClient();
CreateServer();
}
// Define a class to be send
public class ClassA
{
public int VarInt;
public double VarDouble;
}
void CreateServer()
{
// Create the server
var server = new SimplMessageServer();
// Start listening for client connections on loopback end point
server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));
// Wait until a new client has connected
var connectedClient = server.WaitForNewClient();
// Create an object to send
var objectToSend = new ClassA() { VarInt = 2, VarDouble = 2.5 };
// Send it with an implicit descriptor (which is the class name)
Console.WriteLine($"server sending received message: {objectToSend.VarDouble}, {objectToSend.VarInt} with implicit descriptor {typeof(ClassA).Name}");
server.Send(objectToSend, connectedClient);
}
void CreateClient()
{
// Create the client
var client = new SimplMessageClient();
// Make the client connect automatically
client.AutoConnect();
// Create a callback for received data of type classA. This time we use a lambda function instead
client.AddCallBack<ClassA>((receivedMessage) =>
{
// get data from received message cast to ClassA
var receivedObject = receivedMessage.GetContent<ClassA>();
// Notify that the server received data
Console.WriteLine($"Client received message: {receivedObject.VarDouble}, {receivedObject.VarInt}");
});
}
}
}