ServerActivityMessages.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. using KairoEngine.Multiplayer;
  6. namespace KairoEngine.Multiplayer
  7. {
  8. public class ServerActivityMessages : MonoBehaviour
  9. {
  10. public ServerBehaviour server;
  11. public string activityMessageName = "server";
  12. private void Awake()
  13. {
  14. if(server == null) Debug.LogError("Missing ServerBehaviour script in ServerActivityMessages", this.gameObject);
  15. }
  16. private void Start()
  17. {
  18. if(server == null) return;
  19. GenericEvents.StartListening($"{server.eventStreamName}_PortError", OnServerEvent);
  20. GenericEvents.StartListening($"{server.eventStreamName}_Listening", OnServerEvent);
  21. GenericEvents.StartListening($"{server.eventStreamName}_Stopped", OnServerEvent);
  22. GenericEvents.StartListening($"{server.eventStreamName}_ClientConnected", OnServerClientConnectedEvent);
  23. GenericEvents.StartListening($"{server.eventStreamName}_ClientDisconnected", OnServerEvent);
  24. NetMsgEvents.StartListening($"{server.eventStreamName}_InvalidOpCode", OnServerReceivedDataEvent);
  25. NetMsgEvents.StartListening($"{server.eventStreamName}_SendingDisconnectMessageToClient", OnServerReceivedDataEvent);
  26. NetMsgEvents.StartListening($"{server.eventStreamName}_ClientDisconnect", OnServerReceivedDataEvent);
  27. NetMsgEvents.StartListening($"{server.eventStreamName}_HandshakeReceived", OnServerReceivedDataEvent);
  28. NetMsgEvents.StartListening($"{server.eventStreamName}_AcceptingHandshake", OnServerReceivedDataEvent);
  29. }
  30. private void OnDestroy()
  31. {
  32. if(server == null) return;
  33. GenericEvents.StopListening($"{server.eventStreamName}_PortError", OnServerEvent);
  34. GenericEvents.StopListening($"{server.eventStreamName}_Listening", OnServerEvent);
  35. GenericEvents.StopListening($"{server.eventStreamName}_Stopped", OnServerEvent);
  36. GenericEvents.StopListening($"{server.eventStreamName}_ClientConnected", OnServerClientConnectedEvent);
  37. GenericEvents.StopListening($"{server.eventStreamName}_ClientDisconnected", OnServerEvent);
  38. NetMsgEvents.StopListening($"{server.eventStreamName}_InvalidOpCode", OnServerReceivedDataEvent);
  39. NetMsgEvents.StopListening($"{server.eventStreamName}_SendingDisconnectMessageToClient", OnServerReceivedDataEvent);
  40. NetMsgEvents.StopListening($"{server.eventStreamName}_ClientDisconnect", OnServerReceivedDataEvent);
  41. NetMsgEvents.StopListening($"{server.eventStreamName}_HandshakeReceived", OnServerReceivedDataEvent);
  42. NetMsgEvents.StopListening($"{server.eventStreamName}_AcceptingHandshake", OnServerReceivedDataEvent);
  43. }
  44. public void OnServerEvent(string text)
  45. {
  46. ActivityMessageEvents.Send(text, "", server.eventStreamName, activityMessageName, null, 0f, true, false, null, "", "", "");
  47. }
  48. public void OnServerReceivedDataEvent(string text, int clientId, uint code, NetMsg netMsg)
  49. {
  50. ActivityMessageEvents.Send(text, "", server.eventStreamName, activityMessageName, null, 0f, true, false, null, "", "", "");
  51. }
  52. public void OnServerClientConnectedEvent(string text, int connectionId)
  53. {
  54. ActivityMessageEvents.Send(text, "", server.eventStreamName, activityMessageName, null, 0f, true, false, null, "", "", "");
  55. }
  56. }
  57. }