Conventionalized networking scripts.

This commit is contained in:
excitedneon 2017-05-08 03:59:42 +03:00
parent aca14eaae8
commit 7038b46251
6 changed files with 51 additions and 51 deletions

View File

@ -5,8 +5,8 @@ using UnityEngine.Networking;
public class Client : MonoBehaviour {
NetworkClient client;
private bool running = false;
NetworkClient NetClient;
private bool Running = false;
// Use this for initialization
void Start () {
@ -14,37 +14,37 @@ public class Client : MonoBehaviour {
// Update is called once per frame
void Update () {
if (client.isConnected) {
if (NetClient.isConnected) {
}
}
public void LaunchClient(string ip, int port) {
if (running) {
if (Running) {
return;
}
ConnectionConfig config = new ConnectionConfig();
config.AddChannel(QosType.ReliableSequenced);
config.AddChannel(QosType.UnreliableSequenced);
NetworkServer.Configure(config, 10);
ConnectionConfig Config = new ConnectionConfig();
Config.AddChannel(QosType.ReliableSequenced);
Config.AddChannel(QosType.UnreliableSequenced);
NetworkServer.Configure(Config, 10);
client = new NetworkClient();
client.Configure(config, 10);
NetClient = new NetworkClient();
NetClient.Configure(Config, 10);
running = true;
Running = true;
client.RegisterHandler(MsgType.Connect, OnConnected);
client.RegisterHandler(MsgType.Disconnect, OnDisconnected);
client.RegisterHandler(MsgType.Error, OnError);
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
NetClient.RegisterHandler(MsgType.Error, OnError);
client.Connect(ip, port);
NetClient.Connect(ip, port);
Debug.Log("Client launched!");
}
public void OnConnected(NetworkMessage msg) {
Debug.Log("Connected!");
client.Send(PktType.TestMessage, new TextMessage("Hai, I connected!"));
NetClient.Send(PktType.TestMessage, new TextMessage("Hai, I connected!"));
}
public void OnDisconnected(NetworkMessage msg) {
@ -53,7 +53,7 @@ public class Client : MonoBehaviour {
public void OnError(NetworkMessage msg) {
Debug.LogError("Encountered a network error. Shutting down.");
client.Disconnect();
running = false;
NetClient.Disconnect();
Running = false;
}
}

View File

@ -2,21 +2,21 @@
public class TextMessage : MessageBase {
public string message;
public string Message;
public TextMessage(string message) {
this.message = message;
this.Message = message;
}
public TextMessage() {
}
public override void Deserialize(NetworkReader reader) {
message = reader.ReadString();
Message = reader.ReadString();
}
public override void Serialize(NetworkWriter writer) {
writer.Write(message);
writer.Write(Message);
}
}

View File

@ -13,12 +13,12 @@ public class NetworkEstablisher : MonoBehaviour {
[Tooltip("Required field only if StartClient() is used.")]
public InputField IPField;
[Tooltip("Required field only if StartClient() is used.")]
public InputField clientPortField;
public InputField ClientPortField;
[Tooltip("Required field only if StartServer() is used.")]
public InputField serverPortField;
public InputField ServerPortField;
public GameObject worldRoot;
public GameObject WorldRoot;
// Use this for initialization
void Start () {
@ -31,34 +31,34 @@ public class NetworkEstablisher : MonoBehaviour {
}
public void StartClient() {
string ip = IPField.text;
if (ip.Length == 0) {
ip = "localhost";
string IP = IPField.text;
if (IP.Length == 0) {
IP = "localhost";
}
string portText = clientPortField.text;
int port = 3935;
if (portText.Length > 0) {
port = Int32.Parse(portText);
string PortText = ClientPortField.text;
int Port = 3935;
if (PortText.Length > 0) {
Port = Int32.Parse(PortText);
}
StartClient(ip, port);
StartClient(IP, Port);
}
public void StartClient(string ip, int port) {
Client client = worldRoot.AddComponent<Client>();
client.LaunchClient(ip, port);
Client Client = WorldRoot.AddComponent<Client>();
Client.LaunchClient(ip, port);
}
public void StartServer() {
string portText = serverPortField.text;
int port = 3935;
if (portText.Length > 0) {
port = Int32.Parse(portText);
string PortText = ServerPortField.text;
int Port = 3935;
if (PortText.Length > 0) {
Port = Int32.Parse(PortText);
}
StartServer(port);
StartServer(Port);
}
public void StartServer(int port) {
Server server = worldRoot.AddComponent<Server>();
server.LaunchServer(port);
Server Server = WorldRoot.AddComponent<Server>();
Server.LaunchServer(port);
}
}

View File

@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: 430a933ca3f0fa849bc55f725fe9cffc
timeCreated: 1494190350
guid: 91755ff90ed584281ae73cf477fb817e
timeCreated: 1494205063
licenseType: Free
MonoImporter:
serializedVersion: 2

View File

@ -16,10 +16,10 @@ public class Server : MonoBehaviour {
public void LaunchServer(int port) {
ConnectionConfig config = new ConnectionConfig();
config.AddChannel(QosType.ReliableSequenced);
config.AddChannel(QosType.UnreliableSequenced);
NetworkServer.Configure(config, 10);
ConnectionConfig Config = new ConnectionConfig();
Config.AddChannel(QosType.ReliableSequenced);
Config.AddChannel(QosType.UnreliableSequenced);
NetworkServer.Configure(Config, 10);
NetworkServer.Listen(port);
@ -36,9 +36,9 @@ public class Server : MonoBehaviour {
switch(msg.msgType) {
case PktType.TestMessage:
TextMessage textMsg = new TextMessage();
textMsg.Deserialize(msg.reader);
Debug.Log("Received message: " + textMsg.message);
TextMessage TextMsg = new TextMessage();
TextMsg.Deserialize(msg.reader);
Debug.Log("Received message: " + TextMsg.Message);
break;
default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType);