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
        private void OnConnectedToServer(int connectionId)
        {
            if (NetUsers.ContainsKey(connectionId))
            {
                Debug.Log($"@OnConnectedToServer -> userId [{connectionId}] has Re-Connected");
            }
            else
            {
                NetUser newUser = new NetUser()
                {
                    ConnectionID = connectionId
                };
                NetUsers[connectionId] = newUser;
                Debug.Log($"@OnConnectedToServer -> UserId[{connectionId}]");
            }
            // Send CONNECTION_ACK with the connectionId to the client
            ByteStream bytestream = new ByteStream();

            bytestream.Append((byte)NetMessageType.CONNECTION_ACK);
            bytestream.Append(connectionId);
            SendNetMessage(connectionId, ReliableChannel, bytestream.ToArray());
            // Spawn a player instance that represents the new user
            PlayerInstances[connectionId] = NetSpawner.SpawnPlayer(Vector3.zero, Quaternion.identity);
        }
Example #3
0
        private void OnConnectedEvent(int connectionId)
        {
            float posX = Random.Range(-30f,30f);
            float posZ = Random.Range(-30f,30f);
            Vector3 spawnPosition = new Vector3(posX, _PlayerPrefab.transform.position.y, posZ);

            if (NetUsers.ContainsKey(connectionId))
            {
                Debug.Log($"@Receiver.Connect -> userId = [{connectionId}] Re-Conneted");
            }
            else
            {
                var newUser = new NetUser() { ConnectionID = connectionId };
                NetUsers[connectionId] = newUser;
                NetUsers[connectionId].Player = Instantiate(_PlayerPrefab, spawnPosition, Quaternion.identity);
                Debug.Log($"@Receiver.Connect -> userId = [{connectionId}]");
            }

            var byteStream = new ByteStream();
            byteStream.Append((byte)NetMessageType.CONNECTION_ACK);
            byteStream.Append(connectionId);
            byteStream.Append(spawnPosition);
            SendNetMessage(connectionId, ReliableChannel, byteStream.ToArray());
        }