1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- namespace KairoEngine.Multiplayer
- {
- [HideMonoScript]
- public class ClientList : MonoBehaviour
- {
- public ServerBehaviour server;
- public List<ClientData> clients;
- private void OnEnable()
- {
- NetMsgEvents.StartListening($"{server.eventStreamName}_AcceptingHandshake", OnServerAcceptingHandshakeEvent);
- NetMsgEvents.StartListening($"{server.eventStreamName}_ClientDisconnect", OnServerClientDisconnectEvent);
- }
- private void OnDisable()
- {
- NetMsgEvents.StopListening($"{server.eventStreamName}_AcceptingHandshake", OnServerAcceptingHandshakeEvent);
- NetMsgEvents.StartListening($"{server.eventStreamName}_ClientDisconnect", OnServerClientDisconnectEvent);
- }
- private void OnServerAcceptingHandshakeEvent(string text, int clientId, uint code, NetMsg netMsg)
- {
- NetAcceptHandshakeMsg handshake = (NetAcceptHandshakeMsg)netMsg;
- bool registered = false;
- for (int i = 0; i < clients.Count; i++)
- {
- if(clients[i].playerName == handshake.playerName)
- {
- clients[i].connectionId = clientId;
- clients[i].isConnected = true;
- registered = true;
- }
- }
- if(!registered) clients.Add(new ClientData(clientId, handshake.playerName, true));
- }
- private void OnServerClientDisconnectEvent(string text, int clientId, uint code, NetMsg netMsg)
- {
- NetDisconnectMsg disconnectMsg = (NetDisconnectMsg)netMsg;
- for (int i = 0; i < clients.Count; i++)
- {
- if(clients[i].connectionId == clientId)
- {
- clients[i].connectionId = -1;
- clients[i].isConnected = false;
- }
- }
- }
- public ClientData GetClientData(int connectionId)
- {
- for (int i = 0; i < clients.Count; i++)
- {
- if(clients[i].connectionId == connectionId) return clients[i];
- }
- return null;
- }
- }
- }
|