quakeball/Assets/Scripts/Net/CanvasInput.cs

56 lines
1.9 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using NeonTea.Quakeball.TeaNet.Peers;
using NeonTea.Quakeball.Net.Packets;
using System;
namespace NeonTea.Quakeball.Net {
public class CanvasInput : MonoBehaviour, PeerMessageListener {
public Button Host;
public Button Join;
public Button Stop;
public Button Send;
public InputField HostAddr;
public InputField Port;
public InputField MessageField;
public Text TextField;
private static List<string> Stuff = new List<string>();
void Start() {
Host.onClick.AddListener(() => {
string addr = String.IsNullOrWhiteSpace(HostAddr.text) ? "0.0.0.0" : HostAddr.text;
string port = String.IsNullOrWhiteSpace(Port.text) ? "8080" : Port.text;
Debug.Log(addr);
Net.Singleton.StartServer(addr, Int32.Parse(port), this);
});
Join.onClick.AddListener(() => {
string addr = String.IsNullOrWhiteSpace(HostAddr.text) ? "127.0.0.1" : HostAddr.text;
string port = String.IsNullOrWhiteSpace(Port.text) ? "8080" : Port.text;
Net.Singleton.StartClient(addr, Int32.Parse(port), this);
});
Stop.onClick.AddListener(() => {
Net.Singleton.Stop();
});
Send.onClick.AddListener(() => {
if (Net.Singleton.Peer != null && Net.Singleton.Connections.Count > 0) {
HelloPckt pckt = new HelloPckt();
pckt.Text = MessageField.text;
Net.Singleton.Peer.SendReliable(Net.Singleton.Connections[0], pckt);
}
});
}
void Update() {
TextField.text = string.Join("\n", Stuff.ToArray());
}
public void Message(string text) {
Stuff.Add(text);
Debug.Log(text);
}
}
}