2017-05-08 00:59:50 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
public class Client : MonoBehaviour {
|
|
|
|
|
|
2017-05-08 02:59:42 +02:00
|
|
|
|
NetworkClient NetClient;
|
|
|
|
|
private bool Running = false;
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
|
|
|
|
// Use this for initialization
|
|
|
|
|
void Start () {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update () {
|
2017-05-08 02:59:42 +02:00
|
|
|
|
if (NetClient.isConnected) {
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LaunchClient(string ip, int port) {
|
2017-05-08 02:59:42 +02:00
|
|
|
|
if (Running) {
|
2017-05-08 00:59:50 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 02:59:42 +02:00
|
|
|
|
ConnectionConfig Config = new ConnectionConfig();
|
|
|
|
|
Config.AddChannel(QosType.ReliableSequenced);
|
|
|
|
|
Config.AddChannel(QosType.UnreliableSequenced);
|
|
|
|
|
NetworkServer.Configure(Config, 10);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
2017-05-08 02:59:42 +02:00
|
|
|
|
NetClient = new NetworkClient();
|
|
|
|
|
NetClient.Configure(Config, 10);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
2017-05-08 02:59:42 +02:00
|
|
|
|
Running = true;
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
2017-05-08 03:42:18 +02:00
|
|
|
|
NetClient.RegisterHandler(PktType.TextMessage, HandlePacket);
|
|
|
|
|
|
2017-05-08 02:59:42 +02:00
|
|
|
|
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
|
|
|
|
|
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
|
|
|
|
NetClient.RegisterHandler(MsgType.Error, OnError);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
2017-05-08 02:59:42 +02:00
|
|
|
|
NetClient.Connect(ip, port);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
|
|
|
|
Debug.Log("Client launched!");
|
2017-05-08 03:31:32 +02:00
|
|
|
|
Term.Println("Client launched!");
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 03:42:18 +02:00
|
|
|
|
public void HandlePacket(NetworkMessage msg) {
|
|
|
|
|
switch (msg.msgType) {
|
|
|
|
|
case (PktType.TextMessage):
|
|
|
|
|
TextMessage TextMsg = new TextMessage();
|
|
|
|
|
TextMsg.Deserialize(msg.reader);
|
|
|
|
|
Term.Println(TextMsg.Message);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
|
|
|
|
Term.Println("Received an unknown packet, id: " + msg.msgType);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Send(short type, MessageBase message) {
|
|
|
|
|
NetClient.Send(type, message);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 00:59:50 +02:00
|
|
|
|
public void OnConnected(NetworkMessage msg) {
|
|
|
|
|
Debug.Log("Connected!");
|
2017-05-08 03:31:32 +02:00
|
|
|
|
Term.Println("Connected!");
|
2017-05-08 03:42:18 +02:00
|
|
|
|
|
|
|
|
|
Term.AddCommand("send (message)", "Send a message across the vastness of space and time!", (args) => {
|
|
|
|
|
Term.Println("You: " + args[0]);
|
|
|
|
|
Send(PktType.TextMessage, new TextMessage("A Client: " + args[0]));
|
|
|
|
|
});
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnDisconnected(NetworkMessage msg) {
|
|
|
|
|
Debug.Log("Disconnected!");
|
2017-05-08 03:31:32 +02:00
|
|
|
|
Term.Println("Disconnected!");
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnError(NetworkMessage msg) {
|
|
|
|
|
Debug.LogError("Encountered a network error. Shutting down.");
|
2017-05-08 03:31:32 +02:00
|
|
|
|
Term.Println("Encountered a network error. Shutting down.");
|
2017-05-08 02:59:42 +02:00
|
|
|
|
NetClient.Disconnect();
|
|
|
|
|
Running = false;
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|