Skip to content

Commit

Permalink
update examples from readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Nov 27, 2023
1 parent 554b564 commit 741c4c6
Showing 1 changed file with 59 additions and 48 deletions.
107 changes: 59 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,54 @@ $ dotnet build "netly/" -c Release -o "netly/bin/"
<br>

##### Demo
- <sub>[HTTP](#demo)</sub>
- <sub>[TCP](#demo)</sub>
- <sub>[HTTP](#http)</sub>
- <sub>[TCP](#tcp)</sub>
- <sub>[UDP](#demo)</sub>
- <sub>[WebSocket](#demo)</sub>

<br/>

##### HTTP
- <sub><strong>HTTP Client</strong></sub>
```csharp
using System;
using Netly;

var client = new HttpClient();

// error callback
client.OnError((exception) =>
{
// request exception error. when connection doesn't open, null uri,...
});

// success callback
client.OnSuccess((request) =>
{
// get status code
int statusCode = request.StatusCode;
// get server response as plain-text
string bodyAsPlainText = request.Body.PlainText;
});

// EXAMPLE:
// set header
client.Headers.Add("content-type", "multipart/form-data");
// set url query
client.Queries.Add("timeout", "1h");

// create form data
var body = new RequestBody(Enctype.Multipart);
// set filename.
body.Add("name", "Video.mp4");
// set filebuffer.
body.Add("file", new byte[]{ 1, 3, 4, 5, 6, 7, 8, 9, 0 });

// Don't block main thread, run on threadpolls.
// Send POST request.
client.Send("POST", new Uri("http://drive.kezero.com/upload?timeout=1h"), body);
```

- <sub><strong>HTTP Server</strong></sub>
```csharp
using System;
Expand All @@ -141,8 +181,11 @@ $ dotnet build "netly/" -c Release -o "netly/bin/"

server.MapAll("/foo", (request, response) =>
{
// Receive request of all
response.Send(200, $"Hello World. this http method is [{request.Method}]");
// receives request from all http methods.
// EXAMPLE:
// Send response for client.
response.Send(200, $"Hello World. this http method is [{request.Method}]");
});

server.MapGet("/", (request, response) =>
Expand All @@ -154,56 +197,24 @@ $ dotnet build "netly/" -c Release -o "netly/bin/"
{
// received POST request at "/login" path.
// request body on plain text.
string text = request.Body.PlainText;
// get email from http form.
string email = request.Body.Form.GetString("email");
// get password from http from.
string password = request.Body.Form.GetString("password");
// get uploaded file from http form. (<form method="post" enctype="multipart/form-data">).
byte[] picture = request.Body.Form.GetBytes("upload");
// EXAMPLE:
// request body on plain text.
string text = request.Body.PlainText;
// get email from http form.
string email = request.Body.Form.GetString("email");
// get password from http from.
string password = request.Body.Form.GetString("password");
// get uploaded file from http form. (<form method="post" enctype="multipart/form-data">).
byte[] picture = request.Body.Form.GetBytes("upload");
});

server.Open(new Uri("http://localhost:8080"));
```
- <sub><strong>HTTP Client</strong></sub>
```csharp
using System;
using Netly;

var client = new HttpClient();
client.Headers.Add("content-type", "multipart/form-data");
client.Queries.Add("timeout", "1h");

// create form data
var body = new RequestBody(Enctype.Multipart);
body.Add("name", "Video.mp4");
body.Add("file", new byte[]{ 1, 3, 4, 5, 6, 7, 8, 9, 0 });

// error callback
client.OnError((exception) =>
{
// request exception error. when connection doesn't open, null uri,...
});

// success callback
client.OnSuccess((request) =>
{
// get status code
int statusCode = request.StatusCode;
// get server response as plain-text
string bodyAsPlainText = request.Body.PlainText;
});

// Don't block main thread, run on threadpolls.
// Send POST request.
client.Send("POST", new Uri("http://drive.kezero.com/upload?timeout=1h"), body);
```

<br/>

##### TCP Demo
- <sub>``class`` <strong>TcpClient</strong></sub>
##### TCP
- <sub><strong>Tcp Client</strong></sub>
```csharp
using Netly;
using Netly.Core;
Expand Down Expand Up @@ -246,7 +257,7 @@ $ dotnet build "netly/" -c Release -o "netly/bin/"
client.Open(new Host("127.0.0.1", 8080));
```

- <sub>``class`` <strong>TcpServer</strong></sub>
- <sub><strong>Tcp Server</strong></sub>
```csharp
using Netly;
using Netly.Core;
Expand Down

0 comments on commit 741c4c6

Please sign in to comment.