ServerActivityMessages.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. NetMsgEvents.StartListening($"{server.eventStreamName}_PingSent", OnServerReceivedDataEvent);
  30. NetMsgEvents.StartListening($"{server.eventStreamName}_PongReceived", OnServerReceivedDataEvent);
  31. }
  32. private void OnDestroy()
  33. {
  34. if(server == null) return;
  35. GenericEvents.StopListening($"{server.eventStreamName}_PortError", OnServerEvent);
  36. GenericEvents.StopListening($"{server.eventStreamName}_Listening", OnServerEvent);
  37. GenericEvents.StopListening($"{server.eventStreamName}_Stopped", OnServerEvent);
  38. GenericEvents.StopListening($"{server.eventStreamName}_ClientConnected", OnServerClientConnectedEvent);
  39. GenericEvents.StopListening($"{server.eventStreamName}_ClientDisconnected", OnServerEvent);
  40. NetMsgEvents.StopListening($"{server.eventStreamName}_InvalidOpCode", OnServerReceivedDataEvent);
  41. NetMsgEvents.StopListening($"{server.eventStreamName}_SendingDisconnectMessageToClient", OnServerReceivedDataEvent);
  42. NetMsgEvents.StopListening($"{server.eventStreamName}_ClientDisconnect", OnServerReceivedDataEvent);
  43. NetMsgEvents.StopListening($"{server.eventStreamName}_HandshakeReceived", OnServerReceivedDataEvent);
  44. NetMsgEvents.StopListening($"{server.eventStreamName}_AcceptingHandshake", OnServerReceivedDataEvent);
  45. NetMsgEvents.StopListening($"{server.eventStreamName}_PingSent", OnServerReceivedDataEvent);
  46. NetMsgEvents.StopListening($"{server.eventStreamName}_PongReceived", OnServerReceivedDataEvent);
  47. }
  48. public void OnServerEvent(string text)
  49. {
  50. ActivityMessageEvents.Send(text, "", server.eventStreamName, activityMessageName, null, 0f, true, false, null, "", "", "");
  51. }
  52. public void OnServerReceivedDataEvent(string text, int clientId, uint code, NetMsg netMsg)
  53. {
  54. ActivityMessageEvents.Send(text, "", server.eventStreamName, activityMessageName, null, 0f, true, false, null, "", "", "");
  55. }
  56. public void OnServerClientConnectedEvent(string text, int connectionId)
  57. {
  58. ActivityMessageEvents.Send(text, "", server.eventStreamName, activityMessageName, null, 0f, true, false, null, "", "", "");
  59. }
  60. }
  61. }