Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Networking;
- public class Client : 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 const string SERVER_IP = "127.0.0.1";
- private byte reliableChannel;
- private int connectionId;
- private int hostId;
- private byte error;
- private bool isStarted;
- #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);
- // Client only code
- hostId = NetworkTransport.AddHost(topo, 0);
- #if UNITY_WEBGL && !UNITY_EDITOR
- // Web Client
- connectionId = NetworkTransport.Connect(hostId,SERVER_IP,WEB_PORT, 0, out error);
- Debug.Log("Connecting from Web");
- #else
- // Standalone Client
- connectionId = NetworkTransport.Connect(hostId,SERVER_IP,PORT, 0, out error);
- Debug.Log("Connecting from standalone");
- #endif
- Debug.Log(string.Format("Attempting to connect on {0}...", SERVER_IP));
- 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 channelId; // 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("We have connected to the server");
- break;
- case NetworkEventType.DisconnectEvent:
- Debug.Log("We have been disconnected");
- break;
- case NetworkEventType.DataEvent:
- Debug.Log("Data");
- break;
- default:
- case NetworkEventType.BroadcastEvent:
- Debug.Log("Unexpected network event tpye");
- break;
- }
- #region Send
- public void SendServer()
- {
- // This is where we hold our data
- byte[] buffer = new byte[BYTE_SIZE];
- // This is where you would crush your data into a byte[]
- buffer[0] = 255;
- NetworkTransport.Send(hostId,connectionId, reliableChannel, buffer, BYTE_SIZE, out error);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement