Advertisement
Guest User

Untitled

a guest
Apr 26th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. public class Lobby : MonoBehaviour {
  2. UdpSocket socket= UdpKitUnityUtils.CreatePlatformSpecificSocket<LobbySerializer>();
  3. List<UdpConnection>_clients=new List<UdpConnection>();
  4. string _serveraddress = "127.0.0.1:14000";
  5. UdpEvent ev=default(UdpEvent);
  6.  
  7.  
  8. class LobbySerializer:UdpSerializer
  9. {
  10. public override bool Pack (UdpStream stream, ref object o)
  11. {
  12. throw new System.NotImplementedException ();
  13. }
  14. public override bool Unpack (UdpStream stream, ref object o)
  15. {
  16. throw new System.NotImplementedException ();
  17. }
  18. }
  19.  
  20.  
  21. void Awake () {
  22. UdpLog.SetWriter((s, lvl) => Debug.Log(s));
  23. }
  24.  
  25. void OnDestroy () {
  26. socket.Close();
  27. }
  28.  
  29. void Update () {
  30. ev = default(UdpEvent);
  31.  
  32. while (socket.Poll(ref ev)) {
  33. switch (ev.EventType) {
  34. case UdpEventType.Connected:
  35.  
  36. UdpLog.User("Client connect from {0}", ev.Connection.RemoteEndPoint);
  37. _clients.Add (ev.Connection);
  38. ev.Connection.Send ("Welcome in!");
  39.  
  40. break;
  41.  
  42. }
  43. }
  44. }
  45.  
  46. void OnGUI ()
  47. {
  48. GUILayout.BeginVertical();
  49. GUILayout.Label (socket.State.ToString ());
  50. if (socket.State!=UdpSocketState.Running)
  51. {
  52. if (GUILayout.Button("Server"))
  53. socket.Start(new UdpEndPoint(_serveraddress));
  54.  
  55. if (GUILayout.Button("Client")) {
  56. socket.Start(UdpEndPoint.Any);
  57. socket.Connect(new UdpEndPoint(_serveraddress));
  58. }
  59. }
  60. else
  61. {
  62. for(int i=0;i<_clients.Count;i++)
  63. GUILayout.Label (i+" "+_clients[i].UserToken);
  64. if (GUILayout.Button ("Disconnect"))
  65. socket.Close ();
  66. }
  67. GUILayout.EndVertical ();
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement