Cyber/Assets/Scripts/Networking/Client.cs

64 lines
1.7 KiB
C#
Raw Normal View History

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;
// 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) {
}
}
public void LaunchClient(string ip, int port) {
2017-05-08 02:59:42 +02:00
if (Running) {
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 02:59:42 +02:00
NetClient = new NetworkClient();
NetClient.Configure(Config, 10);
2017-05-08 02:59:42 +02:00
Running = true;
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 02:59:42 +02:00
NetClient.Connect(ip, port);
Debug.Log("Client launched!");
Term.Println("Client launched!");
}
public void OnConnected(NetworkMessage msg) {
Debug.Log("Connected!");
Term.Println("Connected!");
2017-05-08 02:59:42 +02:00
NetClient.Send(PktType.TestMessage, new TextMessage("Hai, I connected!"));
}
public void OnDisconnected(NetworkMessage msg) {
Debug.Log("Disconnected!");
Term.Println("Disconnected!");
}
public void OnError(NetworkMessage msg) {
Debug.LogError("Encountered a network error. Shutting down.");
Term.Println("Encountered a network error. Shutting down.");
2017-05-08 02:59:42 +02:00
NetClient.Disconnect();
Running = false;
}
}