You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.0 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. namespace Multiplayer
  6. {
  7. public class ClientManager : MonoBehaviour
  8. {
  9. #region Inspector Variables
  10. [Header("Client Settings")]
  11. [Tooltip("Address server is hosted on")]
  12. [SerializeField]
  13. private string ServerAddress = "127.0.0.1";
  14. [Tooltip("Port to connect to server on")]
  15. [SerializeField]
  16. private int Port = 4444;
  17. [Tooltip("Connects client to server on awake")]
  18. [SerializeField]
  19. private bool ConnectOnStart = false;
  20. public string Name;
  21. #endregion
  22. //Returns if client is current connected to a server
  23. public bool IsConnected { get; private set; }
  24. public byte ID { get; private set; }
  25. //UNET client class which connects to the server;
  26. private NetworkClient uNetClient;
  27. /// <summary>
  28. /// Call to connect client to host
  29. /// </summary>
  30. [ContextMenu("Start Client")]
  31. public void StartClient()
  32. {
  33. StartClient(ServerAddress, Port);
  34. }
  35. /// <summary>
  36. /// Call to connect the client to the host
  37. /// </summary>
  38. /// <param name="ServerAddress">Server Address to connect to</param>
  39. /// <param name="Port">Port to connect on</param>
  40. public void StartClient(string ServerAddress, int Port)
  41. {
  42. //If client is already connected to don't continue
  43. if (IsConnected)
  44. {
  45. Debug.Log("Client already connected to a server. Client needs to disconnect before it can connect to a server");
  46. return;
  47. }
  48. Debug.Log("Attempting to connect to: '" + ServerAddress + "' on port : '" + Port + "'");
  49. uNetClient = new NetworkClient();
  50. RegisterHandlers(uNetClient);
  51. uNetClient.Configure(ChannelConfig.DefaultTopology());
  52. uNetClient.Connect(ServerAddress, Port);
  53. }
  54. /// <summary>
  55. /// Helper function which sets up all necessary handlers for message types
  56. /// </summary>
  57. /// <param name="client"></param>
  58. private void RegisterHandlers(NetworkClient client)
  59. {
  60. client.RegisterHandler(MsgType.Connect, OnConnected);
  61. client.RegisterHandler(MsgType.Disconnect, OnDisconnected);
  62. client.RegisterHandler(LoginMsgID.QueryName, OnRecieveID);
  63. }
  64. private void OnConnected(NetworkMessage msg)
  65. {
  66. IsConnected = true;
  67. ID = (byte)msg.conn.connectionId;
  68. Debug.Log("Successfully connected to server");
  69. }
  70. private void OnDisconnected(NetworkMessage msg)
  71. {
  72. IsConnected = false;
  73. Debug.Log("Disconnected from Server");
  74. }
  75. private void OnRecieveID(NetworkMessage msg)
  76. {
  77. PlayerMsg playerMsg;
  78. if (!Utility.ReadMessage<PlayerMsg>(msg, out playerMsg))
  79. return;
  80. Debug.Log("Recieved ID from server: " + playerMsg.ID);
  81. ID = playerMsg.ID;
  82. SendMessage(LoginMsgID.Name, new StringMsg(ID, Name));
  83. }
  84. /// <summary>
  85. /// Sends message to server
  86. /// </summary>
  87. /// <param name="msgType">Message type, used to determin message handler</param>
  88. /// <param name="msg">The message to send</param>
  89. /// <param name="channel">Which channel to send on, by deafult sends on Reliable</param>
  90. /// <param name="BackLog">If client isn't connected to server add to BackLog, by default true</param>
  91. public void SendMessage(short msgType, MessageBase msg, ServerChannel channel = ServerChannel.Reliable)
  92. {
  93. //if client isn't connected add to backlog
  94. if (IsConnected)
  95. {
  96. Debug.Log("Player not connected to server");
  97. }
  98. uNetClient.SendByChannel(msgType, msg, (int)channel);
  99. }
  100. }
  101. }