Together with my friend, we are making simple chat application. Client is in C# and server in Java. Entire communication is based on WebSockets.
I have problem with designing optimal application flow. Currently it's working like this:
Steps to login:
- Press login button which calls Send method with data as parameter
- Controller has subscribed event callback
- When response is recieved then OnLogin event is fired
Sample code:
void Login()
{
var packet = new LoginDataPacket() { Login = login, Password = SecurityHandler.GetShaWithSalt(password) };
LoginEndpoint.Send(typeof(LoginDataPacket).Name, packet);
LoginView.LoginStarted();
}
void OnLoginStatus(LoginStatusPacket packet)
{
if (packet.Status)
{
LoginView.LoginSuccess();
}
else
{
LoginView.LoginFailed();
}
}
And somewhere in the view:
public void LoginFailed()
{
this.InvokeOnRequired(() =>
{
SetControlsState(true);
labelStatus.ForeColor = Color.Red;
labelStatus.Text = "Failed to login";
});
}
public void LoginSuccess()
{
this.InvokeOnRequired(() =>
{
SetControlsState(true);
labelStatus.ForeColor = Color.Green;
labelStatus.Text = "Login OK!";
Close();
});
}
Everything works fine but...
I would like to know if operation was succesfully or not inside Login method. I can't do this because my network library recieves data using events and also because it would block main thread and then UI would be frozen. Question is how it should be done correctly? I read something about async/await and TaskCompletionSource but i'm not sure how to use it in my case.