Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Lobby : MonoBehaviour {
- UdpSocket socket= UdpKitUnityUtils.CreatePlatformSpecificSocket<LobbySerializer>();
- List<UdpConnection>_clients=new List<UdpConnection>();
- string _serveraddress = "127.0.0.1:14000";
- UdpEvent ev=default(UdpEvent);
- class LobbySerializer:UdpSerializer
- {
- public override bool Pack (UdpStream stream, ref object o)
- {
- throw new System.NotImplementedException ();
- }
- public override bool Unpack (UdpStream stream, ref object o)
- {
- throw new System.NotImplementedException ();
- }
- }
- void Awake () {
- UdpLog.SetWriter((s, lvl) => Debug.Log(s));
- }
- void OnDestroy () {
- socket.Close();
- }
- void Update () {
- ev = default(UdpEvent);
- while (socket.Poll(ref ev)) {
- switch (ev.EventType) {
- case UdpEventType.Connected:
- UdpLog.User("Client connect from {0}", ev.Connection.RemoteEndPoint);
- _clients.Add (ev.Connection);
- ev.Connection.Send ("Welcome in!");
- break;
- }
- }
- }
- void OnGUI ()
- {
- GUILayout.BeginVertical();
- GUILayout.Label (socket.State.ToString ());
- if (socket.State!=UdpSocketState.Running)
- {
- if (GUILayout.Button("Server"))
- socket.Start(new UdpEndPoint(_serveraddress));
- if (GUILayout.Button("Client")) {
- socket.Start(UdpEndPoint.Any);
- socket.Connect(new UdpEndPoint(_serveraddress));
- }
- }
- else
- {
- for(int i=0;i<_clients.Count;i++)
- GUILayout.Label (i+" "+_clients[i].UserToken);
- if (GUILayout.Button ("Disconnect"))
- socket.Close ();
- }
- GUILayout.EndVertical ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement