-
Notifications
You must be signed in to change notification settings - Fork 21
Example: 3 Login
uwee edited this page Feb 10, 2019
·
3 revisions
The client will send its username and password to the server. In the example there is no actual authentication as you will want to put your own solution in place anyway.
The client sends a login request with a callback since its expecting a response to move forward:
public void SendLoginMsg(string username, string password)
{
client.Send((short)MsgId.Login, new LoginMsg() {
AccountName = username,
AccountPassword = Sha256(password) }, (callbackStatus, reader) =>
{
if (callbackStatus == CallbackStatus.Ok)
{
StatusMsg msg = reader.ReadMessage<StatusMsg>();
loginResponse = msg.Text;
loginSucessful = true; //This will always be true for prototyping
Debug.Log(msg.Text);
}
if (callbackStatus == CallbackStatus.Error)
{
Debug.LogError("Callback Error: Login error");
}
if (callbackStatus == CallbackStatus.Timeout)
{
Debug.LogError("Callback Error: Login attempt timed out");
}
});
}Once the message is received by the server it sends a response back via a callback reply:
//This is just an example. No actual authentication happens.
//You would need to replace with your own logic. Perhaps with a DB connection.
private void HandleLoginMsg(InsightNetworkMessage netMsg)
{
LoginMsg message = netMsg.ReadMessage<LoginMsg>();
if (server.logNetworkMessages) { Debug.Log("[InsightServer] - Login Received: " + message.AccountName + " / " + message.AccountPassword); }
registeredUsers.Add(new UserContainer() {
username = message.AccountName,
uniqueId = Guid.NewGuid().ToString(),
connectionId = netMsg.connectionId});
netMsg.Reply((short)MsgId.Status, new StatusMsg() { Text = "Login Sucessful!" });
}