Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3.  
  4. public class Server : 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.  
  11. private byte reliableChannel;
  12. private int hostId;
  13. private int webHostId;
  14.  
  15. private bool isStarted;
  16. private byte error;
  17.  
  18. #region MonoBehaviour
  19. private void Start()
  20. {
  21. DontDestroyOnLoad(gameObject);
  22. Init();
  23. }
  24. private void Update()
  25. {
  26. UpdateMessagePump();
  27. }
  28. #endregion
  29.  
  30. public void Init()
  31. {
  32. NetworkTransport.Init();
  33.  
  34. ConnectionConfig cc = new ConnectionConfig();
  35. reliableChannel = cc.AddChannel(QosType.Reliable);
  36.  
  37. HostTopology topo = new HostTopology(cc, MAX_USER);
  38.  
  39. // Server only code
  40. hostId = NetworkTransport.AddHost(topo, PORT, null);
  41. webHostId = NetworkTransport.AddWebsocketHost(topo, WEB_PORT, null);
  42.  
  43. Debug.Log(string.Format("Opening connection on port {0} and webport {1}",PORT,WEB_PORT));
  44. isStarted = true;
  45. }
  46. public void Shutdown()
  47. {
  48. isStarted = false;
  49. NetworkTransport.Shutdown();
  50. }
  51.  
  52. public void UpdateMessagePump()
  53. {
  54. if(!isStarted)
  55. return;
  56.  
  57. int recHostId; // Is this from Web? Or standalone
  58. int connectionId; // Which user is sending me this?
  59. int channel1Id; // Which lane is he sending that message from
  60.  
  61. byte[] recBuffer = new byte[BYTE_SIZE];
  62. int dataSize;
  63.  
  64. NetworkEventType type = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, BYTE_SIZE, out dataSize, out error);
  65. switch (type)
  66. {
  67. case NetworkEventType.Nothing:
  68. break;
  69.  
  70. case NetworkEventType.ConnectEvent:
  71. Debug.Log(string.Format("User {0} has connected!", connectionId));
  72. break;
  73.  
  74. case NetworkEventType.DisconnectEvent:
  75. Debug.Log("Data");
  76. break;
  77.  
  78. case NetworkEventType.DataEvent:
  79. Debug.Log(string.Format("USer {0} has disconnected :(", connectionId));
  80. break;
  81.  
  82. default:
  83. case NetworkEventType.BroadcastEvent:
  84. Debug.Log("Unexpected network event tpye");
  85. break;
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement