Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Networking;
- public class Server : MonoBehaviour
- {
- private const int MAX_USER = 100;
- private const int PORT = 26000;
- private const int WEB_PORT = 26001;
- private const int BYTE_SIZE = 1024;
- private byte reliableChannel;
- private int hostId;
- private int webHostId;
- private bool isStarted;
- private byte error;
- #region MonoBehaviour
- private void Start()
- {
- DontDestroyOnLoad(gameObject);
- Init();
- }
- private void Update()
- {
- UpdateMessagePump();
- }
- #endregion
- public void Init()
- {
- NetworkTransport.Init();
- ConnectionConfig cc = new ConnectionConfig();
- reliableChannel = cc.AddChannel(QosType.Reliable);
- HostTopology topo = new HostTopology(cc, MAX_USER);
- // Server only code
- hostId = NetworkTransport.AddHost(topo, PORT, null);
- webHostId = NetworkTransport.AddWebsocketHost(topo, WEB_PORT, null);
- Debug.Log(string.Format("Opening connection on port {0} and webport {1}",PORT,WEB_PORT));
- isStarted = true;
- }
- public void Shutdown()
- {
- isStarted = false;
- NetworkTransport.Shutdown();
- }
- public void UpdateMessagePump()
- {
- if(!isStarted)
- return;
- int recHostId; // Is this from Web? Or standalone
- int connectionId; // Which user is sending me this?
- int channel1Id; // Which lane is he sending that message from
- byte[] recBuffer = new byte[BYTE_SIZE];
- int dataSize;
- NetworkEventType type = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, BYTE_SIZE, out dataSize, out error);
- switch (type)
- {
- case NetworkEventType.Nothing:
- break;
- case NetworkEventType.ConnectEvent:
- Debug.Log(string.Format("User {0} has connected!", connectionId));
- break;
- case NetworkEventType.DisconnectEvent:
- Debug.Log("Data");
- break;
- case NetworkEventType.DataEvent:
- Debug.Log(string.Format("USer {0} has disconnected :(", connectionId));
- break;
- default:
- case NetworkEventType.BroadcastEvent:
- Debug.Log("Unexpected network event tpye");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement