using System.Collections; using System.Collections.Generic; using UnityEngine; using KairoEngine.Core; namespace KairoEngine.Multiplayer { public class ServerHandshakeController : MonoBehaviour { public ServerBehaviour server; public NetworkTick networkTick; public string eventStreamName = "ServerEvents"; private void OnEnable() { NetMsgEvents.StartListening($"{eventStreamName}_HandshakeReceived", OnEvent); } private void OnDestroy() { NetMsgEvents.StopListening($"{eventStreamName}_HandshakeReceived", OnEvent); } private void OnEvent(string text, int clientId, uint code, NetMsg netMsg) { if(server == null) return; // Accept the client handshake NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg; NetAcceptHandshakeMsg netAcceptHandshakeMsg = new NetAcceptHandshakeMsg(); if(netHandshakeMsg != null) netAcceptHandshakeMsg.playerName = netHandshakeMsg.playerName; if(networkTick != null) netAcceptHandshakeMsg.tick = (uint)networkTick.tick; Timer.ExecuteRealTime(100, ()=> { server.SendNetMsgToClient(clientId, (uint)NetOpCode.AcceptHandshake, netAcceptHandshakeMsg); }); // Alert all clients of a new connection Timer.ExecuteRealTime(200, ()=> { NetClientConnectedMsg msg = new NetClientConnectedMsg(); msg.playerName = netAcceptHandshakeMsg.playerName; server.SendNetMsgToAllClients((uint)NetOpCode.ClientConnected, msg); }); } } }