quakeball/Assets/Scripts/Networking/CanvasInput.cs
2020-08-08 20:14:34 +03:00

62 lines
2.2 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using NeonTea.Quakeball.TeaNet.Peers;
using NeonTea.Quakeball.Networking.Packets;
using System;
namespace NeonTea.Quakeball.Networking {
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.Quit();
});
Send.onClick.AddListener(() => {
if (Net.Singleton.Instance.Peer != null) {
foreach (ulong uid in Net.Singleton.Instance.Connections) {
HelloPckt pckt = new HelloPckt();
pckt.Text = MessageField.text;
Net.Singleton.Instance.Peer.SendReliableLater(uid, pckt);
Net.Singleton.Instance.Peer.SendReliable(uid, pckt);
}
}
});
}
void Update() {
TextField.text = string.Join("\n", Stuff.ToArray());
}
public void Message(string text) {
Stuff.Add(text);
Debug.Log(text);
}
public void Err(string text) {
Debug.Log(text);
}
}
}