-
Notifications
You must be signed in to change notification settings - Fork 139
Home
HeroWong edited this page Apr 29, 2021
·
4 revisions
SocketIOClient v2.2.0 makes System.Text.Json the default JSON serializer. If you'd like to continue to use Newtonsoft.Json, add the SocketIOClient.Newtonsoft.Json NuGet package and set your JsonSerializer to NewtonsoftJsonSerializer on your SocketIO instance. System.Text.Json is faster and uses less memory.
- 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
var obj = response.GetValue(1);
string message = obj.GetProperty("message").GetString();
int code = obj.GetProperty("code").GetInt32();
});
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);
})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();class MyJsonSerializer : SystemTextJsonSerializer
{
public MyJsonSerializer(int eio) : base(eio) {}
public override JsonSerializerOptions CreateOptions()
{
var options = new JsonSerializerOption();
options.PropertyNameCaseInsensitive = true;
return options;
}
}
// ...
var client = new SocketIO("http://localhost:11000/");
client.JsonSerializer = new MyJsonSerializer(client.Options.EIO);class MyJsonSerializer : NewtonsoftJsonSerializer
{
public MyJsonSerializer(int eio) : base(eio) {}
public override JsonSerializerSettings CreateOptions()
{
return new JsonSerializerSettings
{
ContractResolver = new global::Newtonsoft.Json.Serialization.DefaultContractResolver
{
NamingStrategy = new global::Newtonsoft.Json.Serialization.CamelCaseNamingStrategy()
},
Formatting = Formatting.Indented
};
}
}
// ...
var client = new SocketIO("http://localhost:11000/");
client.JsonSerializer = new MyJsonSerializer(client.Options.EIO);SocketIOClient does not support Windows 7. If your program will run on Windows 7, please uninstall SocketIOClient and install SocketIOClient.Windows7 from NuGet