public async Task<ConnectionCreated> Create(CreateConnectionCommand createConnection) { IDiceConnectionFactory connectionFactory = new DiceConnectionFactory(); try { var game = await GamesContext.GetInstance().GetAsync(createConnection.GameId); if (game != null) { var gameConnection = await game.ConnectionFactory.CreateAsync(new GameConnectionInfo() { HostName = createConnection.HostName, Port = createConnection.Port }); var hostedConnection = new HostedConnection(createConnection.HostName, createConnection.Port, gameConnection); gameConnection.GameDataReceived += (sender, e) => { MessageHubProxy.Invoke("SendMessage", hostedConnection.Id, e.GameData.DataString); }; this.ConnectionHost.Add(hostedConnection); var connection = new Connection() { Id = hostedConnection.Id, HostName = createConnection.HostName, Port = createConnection.Port }; return new ConnectionCreated(hostedConnection.Id, connection); } var responseMessage = new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent(string.Format("Game with id '{0}' was not found.", createConnection.GameId)), }; throw new HttpResponseException(responseMessage); } catch (Exception ex) { var responseMessage = new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent(ex.Message), }; throw new HttpResponseException(responseMessage); } }
private static async Task AddConnectionAsync(MainFormViewModel viewModel) { var createConnection = new CreateConnectionCommand() { GameId = viewModel.CurrentGameId.Value, HostName = viewModel.HostName, Port = viewModel.Port }; var response = await viewModel.HttpClient.PostAsJsonAsync<CreateConnectionCommand>("connection", createConnection); response.EnsureSuccessStatusCode(); var connectionCreated = await response.Content.ReadAsAsync<ConnectionCreated>(); viewModel.Context.Post(state => ((MainFormViewModel)state).AddConnection(new ConnectionViewModel(connectionCreated.Connection)), viewModel); viewModel.CurrentGameId = null; viewModel.HostName = null; viewModel.Port = 0; }