private async Task WaitForConnect(HttpContext context, WebSocket webSocket) { var buffer = new byte[1024 * 4]; WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None); Contract.Assert(!result.CloseStatus.HasValue); try { string json = Encoding.UTF8.GetString(buffer, 0, result.Count); WsMessage message = CatanProxy.Deserialize <WsMessage>(json); if (message.MessageType != CatanWsMessageType.RegisterForGameNotifications) { throw new Exception($"Invalid Message sent: {message} - DataTypeName=CatanWsMessageType"); } await GameController.Games.RegisterWebSocket(context, webSocket); // runs until the end } catch (Exception e) { await webSocket.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(e.ToString()), 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None); } await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); }
public TestHelper(ITestOutputHelper output = null, GameInfo gameInfo = null) { if (!(gameInfo is null)) { GameInfo = gameInfo; } GameName = Guid.NewGuid().ToString(); Proxy = new CatanProxy(); var factory = new CustomWebApplicationFactory <Startup>(); Proxy.Client = factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); }
internal async Task RegisterWebSocket(HttpContext context, WebSocket webSocket) { try { while (true) { WsTcs = new TaskCompletionSource <object>(); await WsTcs.Task; var list = GetLogEntries(); var json = JsonSerializer.SerializeToUtf8Bytes(list, typeof(List <CatanMessage>), CatanProxy.GetJsonOptions()); await webSocket.SendAsync(json, WebSocketMessageType.Text, true, CancellationToken.None); } } catch { //eat it } }
private async void OnTest1(object sdr, RoutedEventArgs rea) { var picker = new PlayerPickerDlg(SavedAppState.Players); var result = await picker.ShowAsync(); if (result != ContentDialogResult.Primary || picker.Player == null) { await StaticHelpers.ShowErrorText("You have to pick a player! Stop messing around Dodgy!"); return; } var playingPlayer = picker.Player; var proxy = new CatanProxy() { // HostName = "http://localhost:5000" HostName = "http://jdlgameservice.azurewebsites.net" }; var existingGames = await proxy.GetGames(); foreach (var game in existingGames) { CatanResult r = await proxy.DeleteGame(game); if (r == null) { this.TraceMessage(proxy.LastErrorString); } } var gameInfo = new GameInfo(); string[] gameNames = new string[] { "Game - 1", "Game - 2" }; List <string> games = null; foreach (var game in gameNames) { await proxy.DeleteGame(game); games = await proxy.CreateGame(game, gameInfo); for (int i = 0; i < 4; i++) { var player = SavedAppState.Players[i]; if (player.PlayerName == "Dodgy") { continue; } var resources = await proxy.JoinGame(game, player.PlayerName); Debug.Assert(resources != null); } } var gamesFromService = await proxy.GetGames(); ServiceGameDlg dlg = new ServiceGameDlg(playingPlayer, SavedAppState.Players, gamesFromService) { HostName = proxy.HostName }; await dlg.ShowAsync(); this.TraceMessage($"Game: {dlg.SelectedGame}"); }