Example #1
0
        private void OnDisconnected(int socketId, int connectionId)
        {
            if (NetUsers.ContainsKey(connectionId) == false)
            {
                Debug.LogError($"@OnDisconnected -> Try to remove userId[{connectionId}] but it doesn't exist");
                return;
            }
            // Remove user and user's instance
            NetUsers.Remove(connectionId);
            NetPlayer pInstance = PlayerInstances[connectionId];

            PlayerInstances.Remove(connectionId);
            Destroy(pInstance.gameObject);
            // Notify all users about thje disconnection
            ByteStream stream = new ByteStream();

            stream.Encode
            (
                (byte)NetMessageType.USER_DISCONNECT,
                connectionId
            );

            BroadcastNetMessage(ReliableChannel, stream.ToArray(), connectionId);
            Debug.Log($"receiver.Disconnect -> Socket[{socketId}], userId[{connectionId}]");
        }
Example #2
0
// ---------------------------------------------------
        // Send disconnected message to all users
        private void OnDisconnectedEvent(int recConnectionId)
        {
            GameObject disconnectPlayer = NetUsers[recConnectionId].Player;
            if(disconnectPlayer != null)
            {
                Destroy(disconnectPlayer);
            }
            NetUsers.Remove(recConnectionId);                                           // Remove disconnected user from server list
            var msg = new ByteStream();                                             
            msg.Encode((byte)NetMessageType.DISCONNECTION_ACK, recConnectionId);        // Create a new disconnection message with user id
            foreach(var user in NetUsers)                                               // Send to all clents
            {
                SendNetMessage(user.Key, ReliableChannel, msg.ToArray());
            }
            Debug.Log($"@Receiver.Disconnect -> userId = [{recConnectionId}]");
        }