1234567891011121314151617181920212223242526272829303132333435363738 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using KairoEngine.Core;
- namespace KairoEngine.Multiplayer
- {
- public class ServerHandshakeController : MonoBehaviour
- {
- public ServerBehaviour server;
- public string eventStreamName = "ServerEvents";
- private void Awake()
- {
- 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 handshakeMsg = (NetHandshakeMsg)netMsg;
- Timer.ExecuteRealTime(100, ()=> {
- server.SendNetMsgToClient(clientId, (uint)NetOpCode.Handshake, handshakeMsg);
- });
- // Alert all clients of a new connection
- Timer.ExecuteRealTime(200, ()=> {
- NetClientConnectedMsg msg = new NetClientConnectedMsg();
- msg.playerName = handshakeMsg.playerName;
- server.SendNetMsgToAllClients((uint)NetOpCode.ClientConnected, msg);
- });
- }
- }
- }
|