2017-05-08 00:59:50 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
2017-05-08 22:29:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This class is used pretty much anywhere in order to make the "first step" of networking.
|
|
|
|
|
/// It adds the proper components to World Root and tells them to start.
|
|
|
|
|
/// </summary>
|
2017-05-08 00:59:50 +02:00
|
|
|
|
public class NetworkEstablisher : MonoBehaviour {
|
|
|
|
|
|
2017-05-08 22:25:53 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Input field for the IP to which connect to.
|
|
|
|
|
/// </summary>
|
2017-05-08 00:59:50 +02:00
|
|
|
|
[Tooltip("Required field only if StartClient() is used.")]
|
|
|
|
|
public InputField IPField;
|
2017-05-08 22:25:53 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Input field for the client port
|
|
|
|
|
/// </summary>
|
2017-05-08 00:59:50 +02:00
|
|
|
|
[Tooltip("Required field only if StartClient() is used.")]
|
2017-05-08 02:59:42 +02:00
|
|
|
|
public InputField ClientPortField;
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
2017-05-08 22:25:53 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Input field for the server port
|
|
|
|
|
/// </summary>
|
2017-05-08 00:59:50 +02:00
|
|
|
|
[Tooltip("Required field only if StartServer() is used.")]
|
2017-05-08 02:59:42 +02:00
|
|
|
|
public InputField ServerPortField;
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
2017-05-08 22:25:53 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// World Root node, a GameObject.
|
|
|
|
|
/// </summary>
|
2017-05-08 02:59:42 +02:00
|
|
|
|
public GameObject WorldRoot;
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
|
|
|
|
// Use this for initialization
|
|
|
|
|
void Start () {
|
2017-05-08 03:31:32 +02:00
|
|
|
|
Term.AddCommand("join", "joins a server at localhost:3935", (args) => {
|
|
|
|
|
StartClient("localhost", 3935);
|
|
|
|
|
});
|
|
|
|
|
Term.AddCommand("join (ip)", "joins a server at given ip and port 3935", (args) => {
|
|
|
|
|
string ip = args[0];
|
|
|
|
|
StartClient(ip, 3935);
|
|
|
|
|
});
|
|
|
|
|
Term.AddCommand("join (ip) (port)", "joins a server at given ip and port", (args) => {
|
|
|
|
|
string ip = args[0];
|
|
|
|
|
int port = 3935;
|
|
|
|
|
int.TryParse(args[1], out port);
|
|
|
|
|
StartClient(ip, port);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Term.AddCommand("host", "host a server at port 3935", (args) => {
|
|
|
|
|
StartServer(3935);
|
|
|
|
|
});
|
|
|
|
|
Term.AddCommand("host (port)", "host a server at given port", (args) => {
|
|
|
|
|
int port = 3935;
|
|
|
|
|
int.TryParse(args[0], out port);
|
|
|
|
|
StartServer(port);
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
2017-05-08 22:25:53 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the client using given input fields. Otherwise functions like <see cref="StartClient(string, int)"/>.
|
|
|
|
|
/// </summary>
|
2017-05-08 00:59:50 +02:00
|
|
|
|
public void StartClient() {
|
2017-05-08 02:59:42 +02:00
|
|
|
|
string IP = IPField.text;
|
|
|
|
|
if (IP.Length == 0) {
|
|
|
|
|
IP = "localhost";
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
2017-05-08 02:59:42 +02:00
|
|
|
|
string PortText = ClientPortField.text;
|
|
|
|
|
int Port = 3935;
|
|
|
|
|
if (PortText.Length > 0) {
|
|
|
|
|
Port = Int32.Parse(PortText);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
2017-05-08 02:59:42 +02:00
|
|
|
|
StartClient(IP, Port);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 22:25:53 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the client with the given ip and port.
|
|
|
|
|
/// This initializes the Client component and launches it properly.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ip">IP used to connect.</param>
|
|
|
|
|
/// <param name="port">port of the host.</param>
|
2017-05-08 00:59:50 +02:00
|
|
|
|
public void StartClient(string ip, int port) {
|
2017-05-08 21:11:05 +02:00
|
|
|
|
WorldRoot.AddComponent<Client>();
|
|
|
|
|
Client.Launch(ip, port);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 22:25:53 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the server using given input fields. Otherwise functions like <see cref="StartServer(int)"/>.
|
|
|
|
|
/// </summary>
|
2017-05-08 00:59:50 +02:00
|
|
|
|
public void StartServer() {
|
2017-05-08 02:59:42 +02:00
|
|
|
|
string PortText = ServerPortField.text;
|
|
|
|
|
int Port = 3935;
|
|
|
|
|
if (PortText.Length > 0) {
|
|
|
|
|
Port = Int32.Parse(PortText);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
2017-05-08 02:59:42 +02:00
|
|
|
|
StartServer(Port);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 22:25:53 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the server using given port.
|
|
|
|
|
/// This initializes the port and launches the server properly.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="port">port used for the server.</param>
|
2017-05-08 00:59:50 +02:00
|
|
|
|
public void StartServer(int port) {
|
2017-05-08 21:11:05 +02:00
|
|
|
|
WorldRoot.AddComponent<Server>();
|
|
|
|
|
Server.Launch(port);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|