Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.IO;
- using System.Net;
- using TNet;
- public class Menu : MonoBehaviour
- {
- public string GameLevel = "Level";
- void Awake()
- {
- }
- void Start()
- {
- if (Application.isPlaying)
- {
- TNManager.Connect("213.144.22.94");
- // Make it possible to use UDP using a random port
- TNManager.StartUDP(Random.Range(10000, 50000));
- InvokeRepeating("Refresh", 2, 2);
- }
- }
- void OnGUI()
- {
- if (TNManager.isConnected)
- {
- if (GUILayout.Button("Join Random Channel"))
- {
- TNManager.JoinRandomChannel(GameLevel, false, 8, "");
- }
- if (GUILayout.Button("Get Channel List"))
- {
- TNManager.client.BeginSend(Packet.RequestChannelList);
- TNManager.client.EndSend();
- }
- }
- }
- /// <summary>
- /// We connected to the GameServer
- /// </summary>
- void OnNetworkConnect(bool success, string message)
- {
- Debug.Log("OnNetworkConnect: " + success + message);
- TNManager.client.packetHandlers[(byte)Packet.ResponseChannelList] = OnChannelList;
- TNManager.StartUDP(Random.Range(10000, 50000)); // Opened UDP port on my server/router
- }
- /// <summary>
- /// Give us the Channellist
- /// </summary>
- void OnChannelList(Packet response, BinaryReader reader, IPEndPoint source)
- {
- int channels = reader.ReadInt32();
- // Read all incoming channel data
- for (int i = 0; i < channels; ++i)
- {
- int id = reader.ReadInt32();
- int players = reader.ReadUInt16();
- int limit = reader.ReadUInt16();
- bool pass = reader.ReadBoolean();
- bool persistent = reader.ReadBoolean();
- string level = reader.ReadString();
- string data = reader.ReadString();
- if (GUI.Button(new Rect(300, 20, 300, 30), "Server: " + level + " Player Count: " + players + " Player Limit: " + limit))
- {
- }
- Debug.Log("channel ID: " + id + " Player Count: " + players + " limit: " + limit + " pass: " + pass + " persistent: " + persistent + " level: " + level + " data: " + data);
- }
- }
- /// <summary>
- /// Refresh the Channellist
- /// </summary>
- void Refresh()
- {
- TNManager.client.BeginSend(Packet.RequestChannelList);
- TNManager.client.EndSend();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement