This repository was archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebsocketTransport.cs
219 lines (182 loc) · 6.88 KB
/
WebsocketTransport.cs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Authentication;
using UnityEngine;
namespace Mirror.Websocket
{
[HelpURL("https://mirror-networking.com/docs/Transports/WebSockets.html")]
public class WebsocketTransport : Transport
{
public const string Scheme = "ws";
public const string SecureScheme = "wss";
protected Client client = new Client();
protected Server server = new Server();
[Header("Transport Settings")]
[Tooltip("Connection Port.")]
public int port = 7778;
[Tooltip("Nagle Algorithm can be disabled by enabling NoDelay.")]
public bool NoDelay = true;
[Header("Secure Sockets (SSL/WSS).")]
[Tooltip("Indicates if SSL/WSS protocol will be used with the PFX Certificate file below.")]
public bool Secure;
[Tooltip("Full path and filename to PFX Certificate file generated from web hosting environment.")]
public string CertificatePath;
[Tooltip("Password for PFX Certificate file above.")]
public string CertificatePassword;
[Tooltip("SSL and TLS Protocols")]
public SslProtocols EnabledSslProtocols = SslProtocols.Default;
public WebsocketTransport()
{
// dispatch the events from the server
server.Connected += (connectionId) => OnServerConnected.Invoke(connectionId);
server.Disconnected += (connectionId) => OnServerDisconnected.Invoke(connectionId);
server.ReceivedData += (connectionId, data) => OnServerDataReceived.Invoke(connectionId, data, Channels.DefaultReliable);
server.ReceivedError += (connectionId, error) => OnServerError.Invoke(connectionId, error);
// dispatch events from the client
client.Connected += () => OnClientConnected.Invoke();
client.Disconnected += () => OnClientDisconnected.Invoke();
client.ReceivedData += (data) => OnClientDataReceived.Invoke(data, Channels.DefaultReliable);
client.ReceivedError += (error) => OnClientError.Invoke(error);
// configure
client.NoDelay = NoDelay;
server.NoDelay = NoDelay;
Debug.Log("Websocket transport initialized!");
}
public override bool Available()
{
// WebSockets should be available on all platforms, including WebGL (automatically) using our included JSLIB code
return true;
}
void OnEnable()
{
server.enabled = true;
client.enabled = true;
}
void OnDisable()
{
server.enabled = false;
client.enabled = false;
}
void LateUpdate()
{
// note: we need to check enabled in case we set it to false
// when LateUpdate already started.
// (https://github.com/vis2k/Mirror/pull/379)
if (!enabled)
return;
// process a maximum amount of client messages per tick
// TODO add clientMaxReceivesPerTick same as telepathy
while (true)
{
// stop when there is no more message
if (!client.ProcessClientMessage())
{
break;
}
// Some messages can disable transport
// If this is disabled stop processing message in queue
if (!enabled)
{
break;
}
}
}
// client
public override bool ClientConnected() => client.IsConnected;
public override void ClientConnect(string host)
{
if (Secure)
{
client.Connect(new Uri($"wss://{host}:{port}"));
}
else
{
client.Connect(new Uri($"ws://{host}:{port}"));
}
}
public override void ClientConnect(Uri uri)
{
if (uri.Scheme != Scheme && uri.Scheme != SecureScheme)
throw new ArgumentException($"Invalid url {uri}, use {Scheme}://host:port or {SecureScheme}://host:port instead", nameof(uri));
if (uri.IsDefaultPort)
{
UriBuilder uriBuilder = new UriBuilder(uri);
uriBuilder.Port = port;
uri = uriBuilder.Uri;
}
client.Connect(uri);
}
public override bool ClientSend(int channelId, ArraySegment<byte> segment)
{
client.Send(segment);
return true;
}
public override void ClientDisconnect() => client.Disconnect();
public override Uri ServerUri()
{
UriBuilder builder = new UriBuilder();
builder.Scheme = Secure ? SecureScheme : Scheme;
builder.Host = Dns.GetHostName();
builder.Port = port;
return builder.Uri;
}
// server
public override bool ServerActive() => server.Active;
public override void ServerStart()
{
server._secure = Secure;
if (Secure)
{
server._secure = Secure;
server._sslConfig = new Server.SslConfiguration
{
Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(CertificatePath, CertificatePassword),
ClientCertificateRequired = false,
CheckCertificateRevocation = false,
EnabledSslProtocols = EnabledSslProtocols
};
}
_ = server.Listen(port);
}
public override bool ServerSend(List<int> connectionIds, int channelId, ArraySegment<byte> segment)
{
// send to all
foreach (int connectionId in connectionIds)
server.Send(connectionId, segment);
return true;
}
public override bool ServerDisconnect(int connectionId)
{
return server.Disconnect(connectionId);
}
public override string ServerGetClientAddress(int connectionId)
{
return server.GetClientAddress(connectionId);
}
public override void ServerStop() => server.Stop();
// common
public override void Shutdown()
{
client.Disconnect();
server.Stop();
}
public override int GetMaxPacketSize(int channelId)
{
// Telepathy's limit is Array.Length, which is int
return int.MaxValue;
}
public override string ToString()
{
if (client.Connecting || client.IsConnected)
{
return client.ToString();
}
if (server.Active)
{
return server.ToString();
}
return "";
}
}
}