Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Add WebSocket Client example in README.md #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 49 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,60 @@ using StackExchange.NetGain.WebSockets;

namespace Example
{
public class Program
{
public static void Main (string[] args)
{
IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 6002);
using(var server = new TcpServer())
public class Program
{
public static void Main (string[] args)
{
server.ProtocolFactory = WebSocketsSelectorProcessor.Default;
server.ConnectionTimeoutSeconds = 60;
server.Received += msg =>
IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 6002);
using(var server = new TcpServer())
{
var conn = (WebSocketConnection)msg.Connection;
string reply = (string)msg.Value + " / " + conn.Host;
Console.WriteLine("[server] {0}", msg.Value);
msg.Connection.Send(msg.Context, reply);
};
server.Start("abc", endpoint);
Console.WriteLine("Server running");
server.ProtocolFactory = WebSocketsSelectorProcessor.Default;
server.ConnectionTimeoutSeconds = 60;
server.Received += msg =>
{
var conn = (WebSocketConnection)msg.Connection;
string reply = (string)msg.Value + " / " + conn.Host;
Console.WriteLine("[server] {0}", msg.Value);
conn.Send(msg.Context, reply);
};

server.Start("abc", endpoint);

Console.WriteLine("Server running");
Console.ReadKey();
}

Console.WriteLine("Server dead; press any key");
Console.ReadKey();
}
Console.WriteLine("Server dead; press any key");
Console.ReadKey();
}
}
}
}
}
```

### WebSocket Client Example ###

```csharp
using System;
using System.Net;
using StackExchange.NetGain;
using StackExchange.NetGain.WebSockets;

namespace Example
{
public class Program
{
public static void Main (string[] args)
{
using (var client = new TcpClient())
{
client.ProtocolFactory = WebSocketClientFactory.Default;
client.Open(new IPEndPoint(IPAddress.Loopback, 6002));

var message = Console.ReadLine();

string resp = (string)client.ExecuteSync(message);
}
}
}
}
```