forked from MidLevel/MLAPI.WebSockets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsWebSocketClient.cs
More file actions
128 lines (106 loc) · 3.57 KB
/
JsWebSocketClient.cs
File metadata and controls
128 lines (106 loc) · 3.57 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
#if JSLIB
using System;
using System.Runtime.InteropServices;
namespace MLAPI.WebSockets
{
internal class JsWebSocketClient : IWebSocketClient
{
[DllImport("__Internal")]
internal static extern int Connect(int instanceId);
[DllImport("__Internal")]
internal static extern int Close(int instanceId, int code, string reason);
[DllImport("__Internal")]
internal static extern int Send(int instanceId, byte[] data, int offset, int size);
[DllImport("__Internal")]
internal static extern int GetState(int instanceId);
private readonly int instanceId;
public OnClientOpenDelegate OnOpen => OnOpenEvent;
public OnClientPayloadDelegate OnPayload => OnPayloadEvent;
public OnClientErrorDelegate OnError => OnErrorEvent;
public OnClientCloseDelegate OnClose => OnCloseEvent;
internal event OnClientOpenDelegate OnOpenEvent;
internal event OnClientPayloadDelegate OnPayloadEvent;
internal event OnClientErrorDelegate OnErrorEvent;
internal event OnClientCloseDelegate OnCloseEvent;
internal JsWebSocketClient(int instanceId)
{
this.instanceId = instanceId;
}
~JsWebSocketClient()
{
WebSocketClientFactory.HandleInstanceDestroy(instanceId);
}
public void Connect()
{
int response = Connect(this.instanceId);
if (response < 0)
{
throw ErrorParser.GetWebSocketExceptionFromErrorCode(response, null);
}
}
public void Close(DisconnectCode code = DisconnectCode.Normal, string reason = null)
{
int response = Close(instanceId, (int)code, reason);
if (response < 0)
{
throw ErrorParser.GetWebSocketExceptionFromErrorCode(response, null);
}
}
public WebSocketState GetState()
{
int state = GetState(instanceId);
if (state < 0)
{
throw ErrorParser.GetWebSocketExceptionFromErrorCode(state, null);
}
switch (state)
{
case 0:
{
return WebSocketState.Connecting;
}
case 1:
{
return WebSocketState.Open;
}
case 2:
{
return WebSocketState.Closing;
}
case 3:
{
return WebSocketState.Closed;
}
default:
{
return WebSocketState.Closed;
}
}
}
public void Send(ArraySegment<byte> payload)
{
int response = Send(instanceId, payload.Array, payload.Offset, payload.Count);
if (response < 0)
{
throw ErrorParser.GetWebSocketExceptionFromErrorCode(response, null);
}
}
public void SetOnOpen(OnClientOpenDelegate action)
{
OnOpenEvent += action;
}
public void SetOnPayload(OnClientPayloadDelegate action)
{
OnPayloadEvent += action;
}
public void SetOnError(OnClientErrorDelegate action)
{
OnErrorEvent += action;
}
public void SetOnClose(OnClientCloseDelegate action)
{
OnCloseEvent += action;
}
}
}
#endif