This repository was archived by the owner on Apr 28, 2024. It is now read-only.
forked from ElectronNET/Electron.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketIOFacade.cs
More file actions
93 lines (79 loc) · 2.4 KB
/
Copy pathSocketIOFacade.cs
File metadata and controls
93 lines (79 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using SocketIOClient;
using SocketIOClient.Newtonsoft.Json;
namespace ElectronNET.API;
internal class SocketIoFacade
{
private readonly SocketIO _socket;
public SocketIoFacade(string uri)
{
_socket = new SocketIO(uri);
var jsonSerializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
});
_socket.JsonSerializer = jsonSerializer;
}
public void Connect()
{
_socket.OnError += (sender, e) =>
{
Console.WriteLine($"BridgeConnector Error: {sender} {e}");
};
_socket.OnConnected += (_, _) =>
{
Console.WriteLine("BridgeConnector connected!");
};
_socket.OnDisconnected += (_, _) =>
{
Console.WriteLine("BridgeConnector disconnected!");
};
_socket.ConnectAsync().GetAwaiter().GetResult();
}
public void On(string eventName, Action action)
{
_socket.On(eventName, response =>
{
Task.Run(action);
});
}
public void On<T>(string eventName, Action<T> action)
{
_socket.On(eventName, response =>
{
var value = response.GetValue<T>();
Task.Run(() => action(value));
});
}
// TODO: Remove this method when SocketIoClient supports object deserialization
public void On(string eventName, Action<object> action)
{
_socket.On(eventName, response =>
{
var value = response.GetValue<object>();
Console.WriteLine($"Called Event {eventName} - data {value}");
Task.Run(() => action(value));
});
}
public void Once<T>(string eventName, Action<T> action)
{
_socket.On(eventName, (socketIoResponse) =>
{
_socket.Off(eventName);
Task.Run(() => action(socketIoResponse.GetValue<T>()));
});
}
public void Off(string eventName)
{
_socket.Off(eventName);
}
public async Task Emit(string eventName, params object[] args)
{
await _socket.EmitAsync(eventName, args);
}
}