public override Task <GameSocket> AcceptGameSocketAsync(string subProtocol) { if (GameSocketFeature == null) { throw new NotSupportedException("GameSockets are not supported"); } return(GameSocketFeature.AcceptAsync(new GameSocketAcceptContext() { SubProtocol = subProtocol })); }
public async Task <GameSocket> ConnectAsync(Uri uri, CancellationToken cancellationToken) { GameSocketFeature gameSocketFeature = null; var contextBuilder = new ProtoContextBuilder(_application, AllowSynchronousIO); contextBuilder.Configure(context => { var request = context.Request; var scheme = uri.Scheme; scheme = (scheme == "ws") ? "http" : scheme; scheme = (scheme == "wss") ? "https" : scheme; request.Scheme = scheme; request.Path = PathString.FromUriComponent(uri); request.PathBase = PathString.Empty; if (request.Path.StartsWithSegments(_pathBase, out var remainder)) { request.Path = remainder; request.PathBase = _pathBase; } request.QueryString = QueryString.FromUriComponent(uri); request.Headers.Add("Connection", new string[] { "Upgrade" }); request.Headers.Add("Upgrade", new string[] { "gamesocket" }); request.Headers.Add("Sec-GameSocket-Version", new string[] { "13" }); request.Headers.Add("Sec-GameSocket-Key", new string[] { CreateRequestKey() }); request.Body = Stream.Null; // GameSocket gameSocketFeature = new GameSocketFeature(context); context.Features.Set <IProtoGameSocketFeature>(gameSocketFeature); ConfigureRequest?.Invoke(context.Request); }); var httpContext = await contextBuilder.SendAsync(cancellationToken); if (httpContext.Response.StatusCode != StatusCodes.Status101SwitchingProtocols) { throw new InvalidOperationException("Incomplete handshake, status code: " + httpContext.Response.StatusCode); } if (gameSocketFeature.ClientGameSocket == null) { throw new InvalidOperationException("Incomplete handshake"); } return(gameSocketFeature.ClientGameSocket); }