public HostPage() { InitializeComponent(); waitingForConnectionLabel.Content = "Waiting for connection on your local ip:"; Task.Run(() => { var ip = NetworkUtils.GetLocalIpAddress(); this.Dispatcher.Invoke(() => { waitingForConnectionLabel.Content = $"Waiting for connection on your local ip: {ip}"; }); }); Task.Run(async() => { var webSocket = await WebSocketConnectionHandler.ListenToWSConnectionAsync("8080"); if (webSocket != null) { this.Dispatcher.Invoke(() => { ConnectionStarted(webSocket); }); } else { this.Dispatcher.Invoke(() => { if (!cancellationTokenSource.IsCancellationRequested) { MessageBox.Show("Erro ao tentar iniciar host, por favor tente abrir a aplicação como administrador."); } this.Cancel(); this.NavigationService.Navigate(new StartingPage()); }); } }, cancellationTokenSource.Token); }
private void TryToConnectWithIp() { string ip = IpTextBox.Text; var task = Task.Run(async() => { var webSocket = await WebSocketConnectionHandler.WaitIpToConnectAsync(ip, "8080"); this.Dispatcher.Invoke(() => { this.ConnectionCallback(webSocket); }); }); var a = task.Status; }
public static async Task HandleConnection(HttpContext context, WebSocket webSocket) { var connection = new WebSocketConnectionHandler(context, webSocket); connection.Subscribe(); var buffer = new byte[1024 * 4]; WebSocketReceiveResult result; do { result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None); }while (!result.CloseStatus.HasValue); connection.Unsubscribe(); await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); }
/// <summary> /// Clean up before exiting page /// </summary> private void WsConnectionClosed() { MessageBox.Show("Connection was closed"); WebSocketConnectionHandler.StopServer(); this.NavigationService.Navigate(new StartingPage()); }
private void Cancel() { cancellationTokenSource.Cancel(); WebSocketConnectionHandler.StopServer(); }