An elegant socket.io client for .NET
- If the server uses socket.io v3, please set EIO explicitly
var client = new SocketIO("http://localhost:11000/", new SocketIOOptions
{
EIO = 4
});
Client:
var client = new SocketIO("http://localhost:11000/");
client.On("hi", response =>
{
string text = response.GetValue<string>();
});
client.OnConnected += async (sender, e) =>
{
await client.EmitAsync("hi", ".net core");
};
await client.ConnectAsync();
Server:
socket.on("hi", name => {
socket.emit("hi", `hi ${name}, You are connected to the server`);
});
Client:
var client = new SocketIO("http://localhost:11000/");
client.OnConnected += async (sender, e) =>
{
await client.EmitAsync("ack", response =>
{
result = response.GetValue();
}, ".net core");
};
await client.ConnectAsync();
Server:
socket.on("ack", (name, fn) => {
fn({
result: true,
message: `ack(${name})`
});
});
Client:
var client = new SocketIO("http://localhost:11000/");
client.OnConnected += async (sender, e) =>
{
await client.EmitAsync("bytes", name, new
{
source = "client001",
bytes = Encoding.UTF8.GetBytes(".net core")
});
};
client.On("bytes", response =>
{
var result = response.GetValue<ByteResponse>();
});
await client.ConnectAsync();
class ByteResponse
{
public string ClientSource { get; set; }
public string Source { get; set; }
[JsonProperty("bytes")]
public byte[] Buffer { get; set; }
}
Server:
socket.on("bytes", (name, data) => {
const bytes = Buffer.from(data.bytes.toString() + " - server - " + name, "utf-8");
socket.emit("bytes", {
clientSource: data.source,
source: "server",
bytes
});
});
Client:
var client = new SocketIO("http://localhost:11000/");
client.OnConnected += async (sender, e) =>
{
await client.EmitAsync("change", new
{
code = 200,
message = "val1"
}, "val2");
};
client.On("change", response =>
{
// You can get the JSON string of the response by calling response.ToString()
// After that you can decide how to parse the response data.
// For example: ["val2", { "code": 200, "message": "val1" }]
string resVal1 = response.GetValue<string>();
ChangeResponse resVal2 = response.GetValue<ChangeResponse>(1);
// If you don't want to create a model, you can parse it like this
string message = response.GetValue(1).Value<string>("message");
int code = response.GetValue(1).Value<int>("code");
// More specific usage: /~https://github.com/jamesnk/newtonsoft.json
});
await client.ConnectAsync();
class ChangeResponse
{
public int Code { get; set; }
public string Message { get; set; }
}
Server:
socket.on("change", (val1, val2) => {
socket.emit("change", val2, val1);
})
SSL and Proxy are related to your program
.NET Core
var proxy = new System.Net.WebProxy("http://example.com");
proxy.Credentials = new NetworkCredential("username", "password");
var socket = client.Socket as ClientWebSocket;
socket.Config = options =>
{
options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
{
Console.WriteLine("SslPolicyErrors: " + sslPolicyErrors);
if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
{
return true;
}
return true;
};
// Set Proxy
options.Proxy = proxy;
};
await client.ConnectAsync();
.NET Framework
.NET Framework is more complicated
var proxy = new System.Net.WebProxy("http://example.com");
proxy.Credentials = new NetworkCredential("username", "password");
if (client.Socket is WebSocketSharpClient)
{
var socket = client.Socket as WebSocketSharpClient;
socket.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls;
socket.Proxy = proxy;
socket.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
{
if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
{
return true;
}
Console.WriteLine(sslPolicyErrors);
return false;
};
}
else
{
var socket = client.Socket as ClientWebSocket;
System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
{
if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
{
return true;
}
Console.WriteLine(sslPolicyErrors);
return false;
};
// Set Proxy
socket.Config = options => options.Proxy = proxy;
}
await client.ConnectAsync();