public async Task Connect(string host, string auth, bool isSecure, string group = null, string code = null) { Hostname = host; Authentification = auth; State = "Verbinde..."; IsActive = true; try { socket = new ClientWebSocket(); socket.Options.AddSubProtocol("chat"); await socket.ConnectAsync(new Uri((isSecure ? "wss://":"ws://") + host), source.Token); int seq = SequenceNumber++; AuthRequest msg = new AuthRequest(auth, seq, group, code); ReceiveTokenSource = new CancellationTokenSource(); ProcessReceivingMessages(); await socket.SendAsync(msg.GetBytes(), WebSocketMessageType.Binary, true, source.Token); IRemoteMessage resp = await WaitForResponse(seq); if (resp is StateResponse) { StateResponse response = (StateResponse)resp; switch (response.Code) { case StateCodes.WrongKey: throw new Exception("Authentifizierung am Server fehlgeschlagen"); case StateCodes.GroupNotFound: throw new Exception("Angegebene Gruppe ist nicht auf dem Server vorhanden"); case StateCodes.WrongGroupKey: throw new Exception("Authentifizierung in der Gruppe fehlgeschlagen"); } } else if (resp is AuthResponse) { AuthResponse response = (AuthResponse)resp; Group = response.Group; State = "Verbunden (" + Group + ")"; } else { throw new Exception("Ungültige Antwort erhalten: " + resp.GetType().ToString()); } } catch (Exception ex) { State = ex.Message; IsActive = false; IsConnected = false; } }