1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using KairoEngine.Core;
- using Sirenix.OdinInspector;
- namespace KairoEngine.Multiplayer
- {
- [HideMonoScript]
- public class ClientHandshakeController : MonoBehaviour
- {
- public ClientBehaviour client;
- public NetworkTick networkTick;
- private bool waitingForHandshake = false;
-
- private void Awake()
- {
- GenericEvents.StartListening($"{client.eventStreamName}_Connected", OnEvent);
- NetMsgEvents.StartListening($"{client.eventStreamName}_HandshakeAccepted", OnHandshakeAccepted);
- }
- private void OnDestroy()
- {
- GenericEvents.StopListening($"{client.eventStreamName}_Connected", OnEvent);
- NetMsgEvents.StopListening($"{client.eventStreamName}_HandshakeAccepted", OnHandshakeAccepted);
- }
- private void OnEvent(string text)
- {
- if(client == null) return;
- NetHandshakeMsg handshakeMsg = new NetHandshakeMsg();
- handshakeMsg.playerName = client.playerName;
- Timer.ExecuteRealTime(100, ()=> {
- waitingForHandshake = true;
- client.SendNetMsgToServer(((uint)NetOpCode.Handshake), handshakeMsg);
- });
- }
- private void OnHandshakeAccepted(string text, int clientId, uint code, NetMsg netMsg)
- {
- if(!waitingForHandshake) return;
- waitingForHandshake = false;
- NetAcceptHandshakeMsg netAcceptHandshakeMsg = (NetAcceptHandshakeMsg)netMsg;
- if(networkTick != null) networkTick.ChangeCurrentTick((int)netAcceptHandshakeMsg.tick);
- }
- }
- }
|