Exemple #1
0
    private void OnMasterServerLoginMessage(object sender, MessageReceivedEventArgs e)
    {
        if (e.Tag != (ushort)MasterServerReplyTags.CharacterLoggedIn)
        {
            return;
        }
        using (var message = e.GetMessage())
        {
            using (var reader = message.GetReader())
            {
                var    messID = reader.ReadUInt16();
                var    data   = reader.ReadSerializable <ServerCharData>();
                ushort sessKey;
                if (sessionTokens.ContainsKey(data.charID))
                {
                    sessKey = sessionTokens[data.charID];
                }
                else
                {
                    sessKey = (ushort)UnityEngine.Random.Range(10, ushort.MaxValue);
                }
                sessionTokens[data.charID] = sessKey;
                charData[data.charID]      = ConnectedPlayer.GetPlayerDataFromJSON(data.jsonData, data.charID);


                Debug.Log($"Client {data.charID} has logged in with session key {sessKey}");

                using (var writer = DarkRiftWriter.Create())
                {
                    writer.Write(messID);
                    writer.Write(new ServerAuthReply {
                        sessToken = sessKey
                    });

                    using (var reply = Message.Create((ushort)MasterServerReplyTags.CharacterLoginAck, writer))
                    {
                        e.Client.SendMessage(reply, SendMode.Reliable);
                    }
                }
            }
        }
    }