ClientActivityMessages.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. namespace KairoEngine.Multiplayer
  6. {
  7. public class ClientActivityMessages : MonoBehaviour
  8. {
  9. public ClientBehaviour client;
  10. public string activityMessageName = "client";
  11. private void Awake()
  12. {
  13. if(client == null) Debug.LogError("Missing ClientBehaviour script in ClientActivityMessages", this.gameObject);
  14. }
  15. private void Start()
  16. {
  17. if(client == null) return;
  18. GenericEvents.StartListening($"{client.eventStreamName}_Connecting", OnClientEvent);
  19. GenericEvents.StartListening($"{client.eventStreamName}_Connected", OnClientEvent);
  20. GenericEvents.StartListening($"{client.eventStreamName}_Disconected", OnClientEvent);
  21. GenericEvents.StartListening($"{client.eventStreamName}_ServerDisconnected", OnClientEvent);
  22. GenericEvents.StartListening($"{client.eventStreamName}_ErrorConnecting", OnClientEvent);
  23. NetMsgEvents.StartListening($"{client.eventStreamName}_InvalidOpCode", OnClientReceivedDataEvent);
  24. NetMsgEvents.StartListening($"{client.eventStreamName}_SendingDisconnectMessageToServer", OnClientReceivedDataEvent);
  25. NetMsgEvents.StartListening($"{client.eventStreamName}_ServerDisconnect", OnClientReceivedDataEvent);
  26. NetMsgEvents.StartListening($"{client.eventStreamName}_SendingHandshake", OnClientReceivedDataEvent);
  27. NetMsgEvents.StartListening($"{client.eventStreamName}_HandshakeAccepted", OnClientReceivedDataEvent);
  28. NetMsgEvents.StartListening($"{client.eventStreamName}_PeerConnected", OnClientReceivedDataEvent);
  29. NetMsgEvents.StartListening($"{client.eventStreamName}_PeerDisconnected", OnClientReceivedDataEvent);
  30. NetMsgEvents.StartListening($"{client.eventStreamName}_PingReceived", OnClientReceivedDataEvent);
  31. NetMsgEvents.StartListening($"{client.eventStreamName}_PongSent", OnClientReceivedDataEvent);
  32. }
  33. private void OnDestroy()
  34. {
  35. if(client == null) return;
  36. GenericEvents.StopListening($"{client.eventStreamName}_Connecting", OnClientEvent);
  37. GenericEvents.StopListening($"{client.eventStreamName}_Connected", OnClientEvent);
  38. GenericEvents.StopListening($"{client.eventStreamName}_Disconected", OnClientEvent);
  39. GenericEvents.StopListening($"{client.eventStreamName}_ServerDisconnected", OnClientEvent);
  40. GenericEvents.StopListening($"{client.eventStreamName}_ErrorConnecting", OnClientEvent);
  41. NetMsgEvents.StopListening($"{client.eventStreamName}_InvalidOpCode", OnClientReceivedDataEvent);
  42. NetMsgEvents.StopListening($"{client.eventStreamName}_SendingDisconnectMessageToServer", OnClientReceivedDataEvent);
  43. NetMsgEvents.StopListening($"{client.eventStreamName}_ServerDisconnect", OnClientReceivedDataEvent);
  44. NetMsgEvents.StopListening($"{client.eventStreamName}_SendingHandshake", OnClientReceivedDataEvent);
  45. NetMsgEvents.StopListening($"{client.eventStreamName}_HandshakeAccepted", OnClientReceivedDataEvent);
  46. NetMsgEvents.StopListening($"{client.eventStreamName}_PingReceived", OnClientReceivedDataEvent);
  47. NetMsgEvents.StopListening($"{client.eventStreamName}_PongSent", OnClientReceivedDataEvent);
  48. }
  49. public void OnClientEvent(string text)
  50. {
  51. ActivityMessageEvents.Send(text, "", client.eventStreamName, activityMessageName, null, 0f, true, false, null, "", "", "");
  52. }
  53. public void OnClientReceivedDataEvent(string text, int clientId, uint code, NetMsg netMsg)
  54. {
  55. ActivityMessageEvents.Send(text, "", client.eventStreamName, activityMessageName, null, 0f, true, false, null, "", "", "");
  56. }
  57. }
  58. }