-
-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathSocketBridgeService.cs
More file actions
61 lines (51 loc) · 1.79 KB
/
SocketBridgeService.cs
File metadata and controls
61 lines (51 loc) · 1.79 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
namespace ElectronNET.Runtime.Services.SocketBridge
{
using ElectronNET.API;
using ElectronNET.Runtime.Data;
using System;
using System.Threading.Tasks;
internal class SocketBridgeService : LifetimeServiceBase
{
private readonly int socketPort;
private readonly string authorization;
private readonly string socketUrl;
private SocketIOConnection socket;
public SocketBridgeService(int socketPort, string authorization)
{
this.socketPort = socketPort;
this.authorization = authorization;
this.socketUrl = $"http://localhost:{this.socketPort}";
}
public int SocketPort => this.socketPort;
internal SocketIOConnection Socket => this.socket;
protected override Task StartCore()
{
this.socket = new SocketIOConnection(this.socketUrl, this.authorization);
this.socket.BridgeConnected += this.Socket_BridgeConnected;
this.socket.BridgeDisconnected += this.Socket_BridgeDisconnected;
Task.Run(this.Connect);
return Task.CompletedTask;
}
protected override Task StopCore()
{
this.socket.Dispose();
return Task.CompletedTask;
}
private void Connect()
{
this.socket.Connect();
if (this.State < LifetimeState.Started)
{
this.TransitionState(LifetimeState.Started);
}
}
private void Socket_BridgeDisconnected(object sender, EventArgs e)
{
this.TransitionState(LifetimeState.Stopped);
}
private void Socket_BridgeConnected(object sender, EventArgs e)
{
this.TransitionState(LifetimeState.Ready);
}
}
}