Skip to content

Commit

Permalink
Readme and examples update.
Browse files Browse the repository at this point in the history
  • Loading branch information
MUN1Z committed Aug 16, 2020
1 parent aca7c8a commit 2bf896a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 30 deletions.
66 changes: 57 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,103 @@
**King Shared**<br/>
<a href="https://www.nuget.org/packages/KingNetwork.Shared/">https://www.nuget.org/packages/KingNetwork.Shared/</a><br/><br/>

# Using the KingServer
# Using the TCP connection on KingServer
```C#
// create and start the server
var server = new KingServer(port: 7171);
server.MessageReceivedHandler = OnMessageReceived;
server.Start();

// implements the callback for MessageReceivedHandler
private void OnMessageReceived(IClient client, KingBuffer kingBuffer)
private void OnMessageReceived(IClient client, KingBufferReader kingBuffer)
{
Console.WriteLine($"Received data from client {client.Id}, data length {kingBuffer.Length()}");
}

// send a message to all clients
using(var kingBuffer = new KingBuffer())
using(var kingBuffer = KingBufferWriter.Create())
{
kingBuffer.WriteString("Test message!");
kingBuffer.Write("Test message!");
server.SendMessageToAll(kingBuffer);
}

// stop the server when you don't need it anymore
server.Stop();
```

# Using the KingClient
# Using the TCP connection on KingClient
```C#
// create and connect the client
var client = new KingClient();
client.MessageReceivedHandler = OnMessageReceived;
client.Connect("127.0.0.1", 7171);

// implements the callback for MessageReceivedHandler
private void OnMessageReceived(KingBuffer kingBuffer)
private void OnMessageReceived(KingBufferReader kingBuffer)
{
Console.WriteLine($"Received data from server, data length {kingBuffer.Length()}");
}

/// send a message to server
using(var kingBuffer = new KingBuffer())
using(var kingBuffer = KingBufferWriter.Create()
{
kingBuffer.WriteString("Test message!");
kingBuffer.Write("Test message!");
client.SendMessage(kingBuffer);
}

// disconnect from the server when we are done
client.Disconnect();
```

# Benchmarks
# Using the WebSocket connection on KingServer
```C#
// create and start the server
var server = new KingServer(port: 7171);
server.MessageReceivedHandler = OnMessageReceived;
server.Start(NetworkListenerType.WSText); // Or NetworkListenerType.WSBinary
// implements the callback for MessageReceivedHandler
private void OnMessageReceived(IClient client, KingBufferReader kingBuffer)
{
Console.WriteLine($"Received data from client {client.Id}, data length {kingBuffer.Length()}");
}

// send a message to all clients
using(var kingBuffer = KingBufferWriter.Create())
{
kingBuffer.Write("Test message!");
server.SendMessageToAll(kingBuffer);
}

// stop the server when you don't need it anymore
server.Stop();
```

# Using the TCP connection on KingClient
```C#
// create and connect the client
var client = new KingClient();
client.MessageReceivedHandler = OnMessageReceived;
client.Connect("127.0.0.1", 7171, NetworkListenerType.WSText); // Or NetworkListenerType.WSBinary
// implements the callback for MessageReceivedHandler
private void OnMessageReceived(KingBufferReader kingBuffer)
{
Console.WriteLine($"Received data from server, data length {kingBuffer.Length()}");
}

/// send a message to server
using(var kingBuffer = KingBufferWriter.Create()
{
kingBuffer.Write("Test message!");
client.SendMessage(kingBuffer);
}

// disconnect from the server when we are done
client.Disconnect();
```

# TCP Benchmarks

**Connections Test**<br/>
We also test only the raw KingNetwork library by spawing 1 server and 1000 clients, each client sending 100 bytes 14 times per second and the server echoing the same message back to each client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using KingNetwork.Shared;
using KingNetwork.SimpleExample.Shared;
using System;
using System.Text;
using System.Threading;

namespace KingNetwork.SimpleExample.Client
Expand All @@ -22,7 +21,7 @@ static void Main(string[] args)
{
try
{
_networkListenerType = NetworkListenerType.WSText;
_networkListenerType = NetworkListenerType.WSBinary;

var client = new KingClient();
client.MessageReceivedHandler = OnMessageReceived;
Expand All @@ -34,13 +33,9 @@ static void Main(string[] args)
using (var buffer = KingBufferWriter.Create())
{
//buffer.WriteBytes(new byte[] { 0, 0, 0, 0 });
if (_networkListenerType != NetworkListenerType.WSText)
buffer.Write(MyPackets.PacketOne);
//buffer.Write((byte)1);
//var bytes = Encoding.GetEncoding("UTF-8").GetBytes("Testinho");
//buffer.Write(bytes);
buffer.Write("Testinho");
client.SendMessage(buffer);
Expand All @@ -64,22 +59,14 @@ private static void OnMessageReceived(KingBufferReader kingBuffer)
try
{
if (_networkListenerType == NetworkListenerType.WSText)
{
Console.WriteLine($"OnMessageReceived: ");

//var text = kingBuffer.ReadAllText();
var text = kingBuffer.ReadString();
Console.WriteLine(text);
}
Console.WriteLine($"OnMessageReceived: {kingBuffer.ReadString()}");
else
{
switch (kingBuffer.ReadMessagePacket<MyPackets>())
{
case MyPackets.PacketOne:
Console.WriteLine("OnMessageReceived for PacketOne");
break;
}
}
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using KingNetwork.Server.Interfaces;
using KingNetwork.Shared;
using KingNetwork.SimpleExample.Shared;
using System.Text;

namespace KingNetwork.SimpleExample.Server
{
Expand All @@ -22,7 +21,7 @@ static void Main(string[] args)
{
try
{
_networkListenerType = NetworkListenerType.WSText;
_networkListenerType = NetworkListenerType.WSBinary;

var server = new KingServer();
server.OnMessageReceivedHandler = OnMessageReceived;
Expand All @@ -49,13 +48,11 @@ private static void OnMessageReceived(IClient client, KingBufferReader kingBuffe
{
Console.WriteLine($"OnMessageReceived from {client.Id}");

//string text = kingBuffer.ReadAllText();
string text = kingBuffer.ReadString();
Console.WriteLine(text);

var writer = KingBufferWriter.Create();

//writer.Write((byte)1);
writer.Write("Testinho2");

client.SendMessage(writer);
Expand All @@ -66,6 +63,14 @@ private static void OnMessageReceived(IClient client, KingBufferReader kingBuffe
{
case MyPackets.PacketOne:
Console.WriteLine($"OnMessageReceived PacketOne from {client.Id}");

var writer = KingBufferWriter.Create();

writer.Write(MyPackets.PacketOne);
writer.Write("Testinho2");

client.SendMessage(writer);

break;
}
}
Expand Down

0 comments on commit 2bf896a

Please sign in to comment.