ClientActivityMessages.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. }
  31. private void OnDestroy()
  32. {
  33. if(client == null) return;
  34. GenericEvents.StopListening($"{client.eventStreamName}_Connecting", OnClientEvent);
  35. GenericEvents.StopListening($"{client.eventStreamName}_Connected", OnClientEvent);
  36. GenericEvents.StopListening($"{client.eventStreamName}_Disconected", OnClientEvent);
  37. GenericEvents.StopListening($"{client.eventStreamName}_ServerDisconnected", OnClientEvent);
  38. GenericEvents.StopListening($"{client.eventStreamName}_ErrorConnecting", OnClientEvent);
  39. NetMsgEvents.StopListening($"{client.eventStreamName}_InvalidOpCode", OnClientReceivedDataEvent);
  40. NetMsgEvents.StopListening($"{client.eventStreamName}_SendingDisconnectMessageToServer", OnClientReceivedDataEvent);
  41. NetMsgEvents.StopListening($"{client.eventStreamName}_ServerDisconnect", OnClientReceivedDataEvent);
  42. NetMsgEvents.StopListening($"{client.eventStreamName}_SendingHandshake", OnClientReceivedDataEvent);
  43. NetMsgEvents.StopListening($"{client.eventStreamName}_HandshakeAccepted", OnClientReceivedDataEvent);
  44. }
  45. public void OnClientEvent(string text)
  46. {
  47. ActivityMessageEvents.Send(text, "", client.eventStreamName, activityMessageName, null, 0f, true, false, null, "", "", "");
  48. }
  49. public void OnClientReceivedDataEvent(string text, int clientId, uint code, NetMsg netMsg)
  50. {
  51. ActivityMessageEvents.Send(text, "", client.eventStreamName, activityMessageName, null, 0f, true, false, null, "", "", "");
  52. }
  53. }
  54. }