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.

100 lines
3.1 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 : MonoSingleton<ClientManager>
  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 LobbyScene;
  21. public string GameScene;
  22. public string Name;
  23. #endregion
  24. //Returns if client is current connected to a server
  25. public bool IsConnected;
  26. public byte ID;
  27. //UNET client class which connects to the server;
  28. public NetworkClient Client { get; private set;}
  29. public ClientLoginManager loginManager;
  30. /// <summary>
  31. /// Call to connect client to host
  32. /// </summary>
  33. [ContextMenu("Start Client")]
  34. public void StartClient()
  35. {
  36. StartClient(ServerAddress, Port);
  37. }
  38. /// <summary>
  39. /// Call to connect the client to the host
  40. /// </summary>
  41. /// <param name="ServerAddress">Server Address to connect to</param>
  42. /// <param name="Port">Port to connect on</param>
  43. public void StartClient(string ServerAddress, int Port)
  44. {
  45. //If client is already connected to don't continue
  46. if (IsConnected)
  47. {
  48. Debug.Log("Client already connected to a server. Client needs to disconnect before it can connect to a server");
  49. return;
  50. }
  51. Debug.Log("Attempting to connect to: '" + ServerAddress + "' on port : '" + Port + "'");
  52. Client = new NetworkClient();
  53. loginManager = new ClientLoginManager(this);
  54. Client.Configure(ChannelConfig.DefaultTopology());
  55. Client.Connect(ServerAddress, Port);
  56. }
  57. /// <summary>
  58. /// Sends message to server
  59. /// </summary>
  60. /// <param name="msgType">Message type, used to determin message handler</param>
  61. /// <param name="msg">The message to send</param>
  62. /// <param name="channel">Which channel to send on, by deafult sends on Reliable</param>
  63. /// <param name="BackLog">If client isn't connected to server add to BackLog, by default true</param>
  64. public void SendMessage(short msgType, MessageBase msg, ServerChannel channel = ServerChannel.Reliable)
  65. {
  66. //if client isn't connected add to backlog
  67. if (!IsConnected)
  68. Debug.Log("Player not connected to server");
  69. //Debug.Log("Sending message to server");
  70. Client.SendByChannel(msgType, msg, (int)channel);
  71. }
  72. public void SendMessage(short msgType)
  73. {
  74. SendMessage(msgType, new PlayerMsg(ID));
  75. }
  76. public void StartGame(NetworkMessage msg)
  77. {
  78. }
  79. }
  80. }