Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3.  
  4. public class Client : MonoBehaviour
  5. {
  6. private const int MAX_USER = 100;
  7. private const int PORT = 26000;
  8. private const int WEB_PORT = 26001;
  9. private const int BYTE_SIZE = 1024;
  10. private const string SERVER_IP = "127.0.0.1";
  11.  
  12. private byte reliableChannel;
  13. private int connectionId;
  14. private int hostId;
  15. private byte error;
  16.  
  17. private bool isStarted;
  18.  
  19. #region MonoBehaviour
  20. private void Start()
  21. {
  22. DontDestroyOnLoad(gameObject);
  23. Init();
  24. }
  25. private void Update()
  26. {
  27. UpdateMessagePump();
  28. }
  29. #endregion
  30.  
  31. public void Init()
  32. {
  33. NetworkTransport.Init();
  34.  
  35. ConnectionConfig cc = new ConnectionConfig();
  36. reliableChannel = cc.AddChannel(QosType.Reliable);
  37.  
  38. HostTopology topo = new HostTopology(cc, MAX_USER);
  39.  
  40. // Client only code
  41. hostId = NetworkTransport.AddHost(topo, 0);
  42.  
  43. #if UNITY_WEBGL && !UNITY_EDITOR
  44. // Web Client
  45. connectionId = NetworkTransport.Connect(hostId,SERVER_IP,WEB_PORT, 0, out error);
  46. Debug.Log("Connecting from Web");
  47. #else
  48. // Standalone Client
  49. connectionId = NetworkTransport.Connect(hostId,SERVER_IP,PORT, 0, out error);
  50. Debug.Log("Connecting from standalone");
  51. #endif
  52.  
  53. Debug.Log(string.Format("Attempting to connect on {0}...", SERVER_IP));
  54. isStarted = true;
  55. }
  56. public void Shutdown()
  57. {
  58. isStarted = false;
  59. NetworkTransport.Shutdown();
  60. }
  61.  
  62. public void UpdateMessagePump()
  63. {
  64. if(!isStarted)
  65. return;
  66.  
  67. int recHostId; // Is this from Web? Or standalone
  68. int connectionId; // Which user is sending me this?
  69. int channelId; // Which lane is he sending that message from
  70.  
  71. byte[] recBuffer = new byte[BYTE_SIZE];
  72. int dataSize;
  73.  
  74. NetworkEventType type = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, BYTE_SIZE, out dataSize, out error);
  75. switch (type)
  76. {
  77. case NetworkEventType.Nothing:
  78. break;
  79.  
  80. case NetworkEventType.ConnectEvent:
  81. Debug.Log("We have connected to the server");
  82. break;
  83.  
  84. case NetworkEventType.DisconnectEvent:
  85. Debug.Log("We have been disconnected");
  86. break;
  87.  
  88. case NetworkEventType.DataEvent:
  89. Debug.Log("Data");
  90. break;
  91.  
  92. default:
  93. case NetworkEventType.BroadcastEvent:
  94. Debug.Log("Unexpected network event tpye");
  95. break;
  96. }
  97.  
  98. #region Send
  99. public void SendServer()
  100. {
  101. // This is where we hold our data
  102. byte[] buffer = new byte[BYTE_SIZE];
  103.  
  104. // This is where you would crush your data into a byte[]
  105. buffer[0] = 255;
  106.  
  107. NetworkTransport.Send(hostId,connectionId, reliableChannel, buffer, BYTE_SIZE, out error);
  108. }
  109. #endregion
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement