Пример #1
0
 private void SendNetworkMessageToAllClients(SNetworkMessage _message)
 {
     foreach (int clientId in connectedClients)
     {
         SendNetworkMessageToClient(clientId, _message);
     }
 }
Пример #2
0
    private void ReceiveServerMessages()
    {
        // show all new messages
        while (server.GetNextMessage(out Telepathy.Message msg))
        {
            switch (msg.eventType)
            {
            case Telepathy.EventType.Connected:
                Debug.Log($"client {msg.connectionId} is Connected");
                connectedClients.Add(msg.connectionId);
                SendNetworkMessageToClient(msg.connectionId, new SNetworkMessage(EMessageType.ConnectionSuccessful, msg.connectionId.ToString()));
                OnNewClient?.Invoke(msg.connectionId);
                break;

            case Telepathy.EventType.Disconnected:
                Debug.Log(msg.connectionId + " Disconnected");
                connectedClients.Remove(msg.connectionId);
                break;

            case Telepathy.EventType.Data:
                SNetworkMessage netMsg = JsonUtility.FromJson <SNetworkMessage>(Encoding.UTF8.GetString(msg.data));
                netMsg.clientID = msg.connectionId;
                //Debug.Log($"client({msg.connectionId}) has send a {netMsg.type} message.");
                SortServerMessages(netMsg);
                break;
            }
        }
    }
Пример #3
0
    private void SortServerMessages(SNetworkMessage _message)
    {
        if (_message.type == EMessageType.Unknown)
        {
            Debug.LogError("Server received an Unknow message");
            return;
        }

        if (_message.type == EMessageType.Instantiate)
        {
            SMessageInstantiate msg = JsonUtility.FromJson <SMessageInstantiate>(_message.JSON);
            msg.GUID = Guid.NewGuid().ToString();
            Debug.Log($"Client {_message.clientID} asked the Server to spawn {msg.prefabName} and register GUID({msg.GUID}) for everyone.");
            SendNetworkMessageToAllClients(new SNetworkMessage(EMessageType.Instantiate, JsonUtility.ToJson(msg)));
        }

        if (_message.type == EMessageType.UpdateTransform)
        {
            SendNetworkMessageToAllClients(_message);
        }

        //only for Chess
        if (_message.type == EMessageType.Grab || _message.type == EMessageType.Ungrab || _message.type == EMessageType.UpdateGrab || _message.type == EMessageType.UpdateHand)
        {
            SendNetworkMessageToAllClients(_message);
        }
    }
Пример #4
0
    private void SortClientMessages(SNetworkMessage _message)
    {
        if (_message.type == EMessageType.Unknown)
        {
            Debug.LogError("Server sent an Unknow message");
            return;
        }

        if (_message.type == EMessageType.ConnectionSuccessful)
        {
            clientID = int.Parse(_message.JSON);
            Debug.Log($"The Server told me I'm client n°{clientID}");
            OnClientConnected?.Invoke();
            isConnected = true;
        }

        if (_message.type == EMessageType.Instantiate)
        {
            SMessageInstantiate msg = JsonUtility.FromJson <SMessageInstantiate>(_message.JSON);
            InstantiateSyncObject(msg.prefabName, msg.GUID, msg.position, msg.rotation, msg.parentName);
            return;
        }

        if (_message.type == EMessageType.UpdateTransform)
        {
            SMessageUpdateTransform msg = JsonUtility.FromJson <SMessageUpdateTransform>(_message.JSON);
            NetworkSynchronizer.Instance.SyncDown(msg.GUID, msg.position, msg.rotation, msg.scale);
            return;
        }

        //only for Chess
        if (_message.type == EMessageType.Grab)
        {
            NetworkSynchronizer.Instance.GetSMBByGUID(_message.JSON).GetComponent <Piece>().Grab();
        }
        if (_message.type == EMessageType.Ungrab)
        {
            NetworkSynchronizer.Instance.GetSMBByGUID(_message.JSON).GetComponent <Piece>().UnGrab();
        }
        if (_message.type == EMessageType.UpdateGrab)
        {
            SMessageVector3 msg = JsonUtility.FromJson <SMessageVector3>(_message.JSON);
            NetworkSynchronizer.Instance.GetSMBByGUID(msg.GUID).GetComponent <Piece>().SyncDownGrab(msg.vector);
        }
        if (_message.type == EMessageType.UpdateHand)
        {
            NetworkSynchronizer.Instance.SyncHandsDown(JsonUtility.FromJson <SMessageHand>(_message.JSON));
        }
    }
Пример #5
0
    private void ReceiveClientMessages()
    {
        // show all new messages
        while (client.GetNextMessage(out Telepathy.Message msg))
        {
            switch (msg.eventType)
            {
            case Telepathy.EventType.Connected:
                Debug.Log(msg.connectionId + " Connected");
                break;

            case Telepathy.EventType.Disconnected:
                Debug.Log(msg.connectionId + " Disconnected");
                break;

            case Telepathy.EventType.Data:
                SNetworkMessage netMsg = JsonUtility.FromJson <SNetworkMessage>(Encoding.UTF8.GetString(msg.data));
                //Debug.Log($"server has send a {netMsg.type} message.");
                SortClientMessages(netMsg);
                break;
            }
        }
    }
Пример #6
0
 public void SendNetworkMessageToServer(SNetworkMessage _message)
 {
     client.Send(Encoding.UTF8.GetBytes(JsonUtility.ToJson(_message)));
 }
Пример #7
0
 private void SendNetworkMessageToClient(int _clientID, SNetworkMessage _message)
 {
     server.Send(_clientID, Encoding.UTF8.GetBytes(JsonUtility.ToJson(_message)));
 }