public void Connect(string host, int port, string username, string password, bool createNewUser) { if (networkClient != null) { Disconnect(); } networkClient = NetworkClient.Connect(host, port); if (createNewUser) { networkClient.WriteMessage(Message.Create(MessageType.CreateUserRequest, new UserInfo() { Username = username, Password = password })); var createUserResponse = networkClient.ReadMessage().GetData<SimpleResponse>(); if (!createUserResponse.Success) { OnConnected(false, createUserResponse.Message); return; } } networkClient.WriteMessage(Message.Create(MessageType.CredentialRequest, new UserInfo() { Username = username, Password = password })); var response = networkClient.ReadMessage().GetData<UserCredentialResponse>(); if (response.IsAuthenticated) { networkClient.UserSession = response.UserSession; Task.Factory.StartNew(() => { StartReading(); // handle disconnecting client if client is in bad state Disconnect(); }, TaskCreationOptions.LongRunning); soundEngine.Record(); OnConnected(true, response.Message); networkClient.WriteMessage(Message.Create(MessageType.GetAllChannelsRequest)); } else { OnConnected(false, response.Message); networkClient.Disconnect(); } }
private void GetAllChannelsRequestHandler(NetworkClient client, Message message) { client.WriteMessage(Message.Create(MessageType.GetAllChannelsResponse, channelSessions)); }
private void CredentialRequestHandlerSuccess(NetworkClient client, UserSession userSession) { userSession.ConnectionId = Guid.NewGuid(); client.UserSession = userSession; AddUserToChannel(userSession, 0); var response = new UserCredentialResponse(); response.UserSession = userSession; response.IsAuthenticated = true; response.Message = "Great success!"; BroadcastAll(client, Message.Create(MessageType.UserConnected, userSession)); client.WriteMessage(Message.Create(MessageType.CredentialResponse, response)); }
private void CredentialRequestHandlerFail(NetworkClient client) { var response = new UserCredentialResponse(); response.IsAuthenticated = false; response.Message = "Authentication failed. Either invalid username or password"; client.WriteMessage(Message.Create(MessageType.CredentialResponse, response)); }
private void CreateUserRequestHandler(NetworkClient client, Message message) { try { userService.CreateUser(message.GetData<UserInfo>()); client.WriteMessage(Message.Create(MessageType.CreateUserResponse, new SimpleResponse() { Success = true, Message = "User created" })); } catch (Exception ex) { client.WriteMessage(Message.Create(MessageType.CreateUserResponse, new SimpleResponse() { Success = false, Message = ex.Message })); } }