-
-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy pathSocketIOConnection.cs
More file actions
156 lines (132 loc) · 3.97 KB
/
SocketIOConnection.cs
File metadata and controls
156 lines (132 loc) · 3.97 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#pragma warning disable IDE0130 // Namespace does not match folder structure
// ReSharper disable once CheckNamespace
namespace ElectronNET.API;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ElectronNET.API.Bridge;
using ElectronNET.API.Serialization;
using SocketIO.Serializer.SystemTextJson;
using SocketIO = SocketIOClient.SocketIO;
using SocketIOOptions = SocketIOClient.SocketIOOptions;
internal class SocketIOConnection : ISocketConnection
{
private readonly SocketIO _socket;
private readonly object _lockObj = new object();
private bool _isDisposed;
public SocketIOConnection(string uri, string authorization)
{
var opts = string.IsNullOrEmpty(authorization) ? new SocketIOOptions() : new SocketIOOptions
{
ExtraHeaders = new Dictionary<string, string>
{
["authorization"] = authorization
},
};
_socket = new SocketIO(uri, opts);
_socket.Serializer = new SystemTextJsonSerializer(ElectronJson.Options);
// Use default System.Text.Json serializer from SocketIOClient.
// Outgoing args are normalized to camelCase via SerializeArg in Emit.
}
public event EventHandler BridgeDisconnected;
public event EventHandler BridgeConnected;
public void Connect()
{
this.CheckDisposed();
_socket.OnError += (sender, e) => { Console.WriteLine($"BridgeConnector Error: {sender} {e}"); };
_socket.OnConnected += (_, _) =>
{
Console.WriteLine("BridgeConnector connected!");
this.BridgeConnected?.Invoke(this, EventArgs.Empty);
};
_socket.OnDisconnected += (_, _) =>
{
Console.WriteLine("BridgeConnector disconnected!");
this.BridgeDisconnected?.Invoke(this, EventArgs.Empty);
};
_socket.ConnectAsync().GetAwaiter().GetResult();
}
public void On(string eventName, Action action)
{
this.CheckDisposed();
lock (_lockObj)
{
_socket.On(eventName, _ => { Task.Run(action); });
}
}
public void On<T>(string eventName, Action<T> action)
{
this.CheckDisposed();
lock (_lockObj)
{
_socket.On(eventName, response =>
{
var value = response.GetValue<T>();
Task.Run(() => action(value));
});
}
}
public void Once(string eventName, Action action)
{
this.CheckDisposed();
lock (_lockObj)
{
_socket.On(eventName, _ =>
{
this.Off(eventName);
Task.Run(action);
});
}
}
public void Once<T>(string eventName, Action<T> action)
{
this.CheckDisposed();
lock (_lockObj)
{
_socket.On(eventName, (socketIoResponse) =>
{
this.Off(eventName);
Task.Run(() => action(socketIoResponse.GetValue<T>()));
});
}
}
public void Off(string eventName)
{
if (_isDisposed)
{
return;
}
lock (_lockObj)
{
_socket.Off(eventName);
}
}
public async Task Emit(string eventName, params object[] args)
{
if (!_isDisposed)
{
await _socket.EmitAsync(eventName, args).ConfigureAwait(false);
}
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_isDisposed = true;
_socket.Dispose();
}
}
private void CheckDisposed()
{
if (this._isDisposed)
{
throw new ObjectDisposedException(nameof(SocketIOConnection));
}
}
}