12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace KairoEngine.Multiplayer
- {
- public class PeerList : MonoBehaviour
- {
- public ClientBehaviour client;
- public List<ClientData> peers;
- private void OnEnable()
- {
- NetMsgEvents.StartListening($"{client.eventStreamName}_PeerConnected", OnPeerConnect);
- NetMsgEvents.StartListening($"{client.eventStreamName}_PeerDisconnected", OnPeerDisconnect);
- }
- private void OnDisable()
- {
- NetMsgEvents.StopListening($"{client.eventStreamName}_PeerConnected", OnPeerConnect);
- NetMsgEvents.StopListening($"{client.eventStreamName}_PeerDisconnected", OnPeerDisconnect);
- }
- private void OnPeerConnect(string text, int clientId, uint code, NetMsg netMsg)
- {
- NetClientConnectedMsg msg = (NetClientConnectedMsg)netMsg;
- for (int i = 0; i < peers.Count; i++)
- {
- if(peers[i].playerName == msg.playerName)
- {
- return;
- }
- }
- if(msg.playerName == client.playerName) return;
- peers.Add(new ClientData(-1, msg.playerName, true));
-
- }
- private void OnPeerDisconnect(string text, int clientId, uint code, NetMsg netMsg)
- {
- NetClientDisconnectedMsg msg = (NetClientDisconnectedMsg)netMsg;
- for (int i = 0; i < peers.Count; i++)
- {
- if(peers[i].playerName == msg.playerName)
- {
- peers.RemoveAt(i);
- return;
- }
- }
- }
- }
- }
|