quakeball/Assets/Scripts/Networking/CanvasInput.cs

62 lines
2.2 KiB
C#
Raw Normal View History

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 {
2020-08-05 03:21:04 +02:00
public class CanvasInput : MonoBehaviour, PeerMessageListener {
public Button Host;
public Button Join;
2020-08-05 03:21:04 +02:00
public Button Stop;
public Button Send;
public InputField HostAddr;
public InputField Port;
public InputField MessageField;
2020-08-05 03:21:04 +02:00
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);
2020-08-08 19:14:34 +02:00
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;
2020-08-08 19:14:34 +02:00
Net.Singleton.StartClient(addr, Int32.Parse(port), "", this);
});
2020-08-05 03:21:04 +02:00
Stop.onClick.AddListener(() => {
Net.Quit();
2020-08-05 03:21:04 +02:00
});
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);
}
}
});
2020-08-05 03:21:04 +02:00
}
void Update() {
TextField.text = string.Join("\n", Stuff.ToArray());
}
public void Message(string text) {
Stuff.Add(text);
Debug.Log(text);
}
2020-08-05 21:54:53 +02:00
public void Err(string text) {
Debug.Log(text);
}
}
}