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.

114 lines
3.7 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. }
  63. private void OnConnected(NetworkMessage msg)
  64. {
  65. IsConnected = true;
  66. ID = (byte)msg.conn.connectionId;
  67. Debug.Log("Successfully connected to server");
  68. Debug.Log("Connection ID: " + msg.conn.connectionId);
  69. SendMessage(LoginMsgID.Name, new StringMsg(ID, Name));
  70. }
  71. private void OnDisconnected(NetworkMessage msg)
  72. {
  73. IsConnected = false;
  74. Debug.Log("Disconnected from Server");
  75. }
  76. /// <summary>
  77. /// Sends message to server
  78. /// </summary>
  79. /// <param name="msgType">Message type, used to determin message handler</param>
  80. /// <param name="msg">The message to send</param>
  81. /// <param name="channel">Which channel to send on, by deafult sends on Reliable</param>
  82. /// <param name="BackLog">If client isn't connected to server add to BackLog, by default true</param>
  83. public void SendMessage(short msgType, MessageBase msg, ServerChannel channel = ServerChannel.Reliable)
  84. {
  85. //if client isn't connected add to backlog
  86. if (IsConnected)
  87. {
  88. Debug.Log("Player not connected to server");
  89. }
  90. uNetClient.SendByChannel(msgType, msg, (int)channel);
  91. }
  92. }
  93. }