-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new websocket transport (#156)
* feat: new websocket transport * Remove old websocket transport * fix closing the conneciton when retrieving end point
- Loading branch information
Showing
14 changed files
with
215 additions
and
757 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Assets/Mirror/Runtime/Transport/Websocket/AsyncWsTransport.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Net.WebSockets; | ||
using System.Threading.Tasks; | ||
using Ninja.WebSockets; | ||
|
||
namespace Mirror.Websocket | ||
{ | ||
public class AsyncWsTransport : AsyncTransport | ||
{ | ||
public int Port = 7778; | ||
|
||
private TcpListener listener; | ||
private readonly IWebSocketServerFactory webSocketServerFactory = new WebSocketServerFactory(); | ||
|
||
public override async Task<IConnection> AcceptAsync() | ||
{ | ||
try | ||
{ | ||
TcpClient tcpClient = await listener.AcceptTcpClientAsync(); | ||
var options = new WebSocketServerOptions { KeepAliveInterval = TimeSpan.FromSeconds(30), SubProtocol = "binary" }; | ||
|
||
Stream stream = tcpClient.GetStream(); | ||
|
||
WebSocketHttpContext context = await webSocketServerFactory.ReadHttpHeaderFromStreamAsync(tcpClient, stream); | ||
|
||
WebSocket webSocket = await webSocketServerFactory.AcceptWebSocketAsync(context, options); | ||
return new WebsocketConnection(webSocket); | ||
} | ||
catch (ObjectDisposedException) | ||
{ | ||
// expected, the connection was closed | ||
return null; | ||
} | ||
} | ||
|
||
public override async Task<IConnection> ConnectAsync(Uri uri) | ||
{ | ||
var options = new WebSocketClientOptions | ||
{ | ||
NoDelay = true, | ||
KeepAliveInterval = TimeSpan.Zero, | ||
SecWebSocketProtocol = "binary" | ||
}; | ||
|
||
if (uri.IsDefaultPort) | ||
{ | ||
var builder = new UriBuilder(uri) | ||
{ | ||
Port = Port | ||
}; | ||
uri = builder.Uri; | ||
} | ||
|
||
var clientFactory = new WebSocketClientFactory(); | ||
WebSocket webSocket = await clientFactory.ConnectAsync(uri, options); | ||
|
||
return new WebsocketConnection(webSocket); | ||
} | ||
|
||
public override void Disconnect() | ||
{ | ||
listener.Stop(); | ||
} | ||
|
||
public override Task ListenAsync() | ||
{ | ||
listener = TcpListener.Create(Port); | ||
listener.Server.NoDelay = true; | ||
listener.Start(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public override Uri ServerUri() | ||
{ | ||
var builder = new UriBuilder | ||
{ | ||
Host = Dns.GetHostName(), | ||
Port = this.Port, | ||
Scheme = "ws" | ||
}; | ||
|
||
return builder.Uri; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...untime/Transport/Websocket/Client.cs.meta → ...nsport/Websocket/AsyncWsTransport.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.