-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cs
78 lines (64 loc) · 2.35 KB
/
Server.cs
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
77
78
using System;
namespace BladeServer
{
/// <summary>
/// Your business class, acting as the server
/// </summary>
class Server
{
// Variable holding the blade Server
blade.Server _server;
// Var holding the client connected. You can of course register clients in a list
blade.ClientConnection _clientConnection;
public Server()
{
string ip = "127.0.0.1";
int port = 1971;
// Instantiating the blade server. DO NOT forget the MsgHandler
_server = new blade.Server(ip, port, MsgHandler);
DoSomething();
}
/// <summary>
/// Will be called each time a message is received over the network.
/// Add your parsing logic here.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MsgHandler(object sender, blade.ClientConnectionEventArgs e)
{
// Access to the message
string receivedMsg = e.clientConnection.Queue.Data.Dequeue();
Console.WriteLine("Server received: {0}", receivedMsg);
if (receivedMsg.Equals("CONNECT")) {
// Access to the client that sent the message
_clientConnection = e.clientConnection;
}
if (receivedMsg.Equals("DISCONNECT")) {
_clientConnection = null;
}
}
/// <summary>
/// Main thread and process.
/// We can send messages from here.
/// </summary>
private void DoSomething()
{
bool quit = false;
while (!quit) {
Console.WriteLine("Send a sentence to client or type \"QUIT\" to exit :");
string input = Console.ReadLine();
if (input.Equals("QUIT")) {
System.Environment.Exit(0);
} else {
if (_clientConnection == null) {
Console.WriteLine("Please start a client");
} else {
// Call Send() to send a message to a specific client
_server.Send(_clientConnection, input);
Console.WriteLine("Server sent \"{0}\" to client", input);
}
}
}
}
}
}