Conventionalized networking scripts.
This commit is contained in:
parent
aca14eaae8
commit
7038b46251
@ -5,8 +5,8 @@ using UnityEngine.Networking;
|
|||||||
|
|
||||||
public class Client : MonoBehaviour {
|
public class Client : MonoBehaviour {
|
||||||
|
|
||||||
NetworkClient client;
|
NetworkClient NetClient;
|
||||||
private bool running = false;
|
private bool Running = false;
|
||||||
|
|
||||||
// Use this for initialization
|
// Use this for initialization
|
||||||
void Start () {
|
void Start () {
|
||||||
@ -14,37 +14,37 @@ public class Client : MonoBehaviour {
|
|||||||
|
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
void Update () {
|
void Update () {
|
||||||
if (client.isConnected) {
|
if (NetClient.isConnected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LaunchClient(string ip, int port) {
|
public void LaunchClient(string ip, int port) {
|
||||||
if (running) {
|
if (Running) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionConfig config = new ConnectionConfig();
|
ConnectionConfig Config = new ConnectionConfig();
|
||||||
config.AddChannel(QosType.ReliableSequenced);
|
Config.AddChannel(QosType.ReliableSequenced);
|
||||||
config.AddChannel(QosType.UnreliableSequenced);
|
Config.AddChannel(QosType.UnreliableSequenced);
|
||||||
NetworkServer.Configure(config, 10);
|
NetworkServer.Configure(Config, 10);
|
||||||
|
|
||||||
client = new NetworkClient();
|
NetClient = new NetworkClient();
|
||||||
client.Configure(config, 10);
|
NetClient.Configure(Config, 10);
|
||||||
|
|
||||||
running = true;
|
Running = true;
|
||||||
|
|
||||||
client.RegisterHandler(MsgType.Connect, OnConnected);
|
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
|
||||||
client.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
||||||
client.RegisterHandler(MsgType.Error, OnError);
|
NetClient.RegisterHandler(MsgType.Error, OnError);
|
||||||
|
|
||||||
client.Connect(ip, port);
|
NetClient.Connect(ip, port);
|
||||||
|
|
||||||
Debug.Log("Client launched!");
|
Debug.Log("Client launched!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnConnected(NetworkMessage msg) {
|
public void OnConnected(NetworkMessage msg) {
|
||||||
Debug.Log("Connected!");
|
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) {
|
public void OnDisconnected(NetworkMessage msg) {
|
||||||
@ -53,7 +53,7 @@ public class Client : MonoBehaviour {
|
|||||||
|
|
||||||
public void OnError(NetworkMessage msg) {
|
public void OnError(NetworkMessage msg) {
|
||||||
Debug.LogError("Encountered a network error. Shutting down.");
|
Debug.LogError("Encountered a network error. Shutting down.");
|
||||||
client.Disconnect();
|
NetClient.Disconnect();
|
||||||
running = false;
|
Running = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,21 +2,21 @@
|
|||||||
|
|
||||||
public class TextMessage : MessageBase {
|
public class TextMessage : MessageBase {
|
||||||
|
|
||||||
public string message;
|
public string Message;
|
||||||
|
|
||||||
public TextMessage(string message) {
|
public TextMessage(string message) {
|
||||||
this.message = message;
|
this.Message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TextMessage() {
|
public TextMessage() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Deserialize(NetworkReader reader) {
|
public override void Deserialize(NetworkReader reader) {
|
||||||
message = reader.ReadString();
|
Message = reader.ReadString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Serialize(NetworkWriter writer) {
|
public override void Serialize(NetworkWriter writer) {
|
||||||
writer.Write(message);
|
writer.Write(Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,12 +13,12 @@ public class NetworkEstablisher : MonoBehaviour {
|
|||||||
[Tooltip("Required field only if StartClient() is used.")]
|
[Tooltip("Required field only if StartClient() is used.")]
|
||||||
public InputField IPField;
|
public InputField IPField;
|
||||||
[Tooltip("Required field only if StartClient() is used.")]
|
[Tooltip("Required field only if StartClient() is used.")]
|
||||||
public InputField clientPortField;
|
public InputField ClientPortField;
|
||||||
|
|
||||||
[Tooltip("Required field only if StartServer() is used.")]
|
[Tooltip("Required field only if StartServer() is used.")]
|
||||||
public InputField serverPortField;
|
public InputField ServerPortField;
|
||||||
|
|
||||||
public GameObject worldRoot;
|
public GameObject WorldRoot;
|
||||||
|
|
||||||
// Use this for initialization
|
// Use this for initialization
|
||||||
void Start () {
|
void Start () {
|
||||||
@ -31,34 +31,34 @@ public class NetworkEstablisher : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void StartClient() {
|
public void StartClient() {
|
||||||
string ip = IPField.text;
|
string IP = IPField.text;
|
||||||
if (ip.Length == 0) {
|
if (IP.Length == 0) {
|
||||||
ip = "localhost";
|
IP = "localhost";
|
||||||
}
|
}
|
||||||
string portText = clientPortField.text;
|
string PortText = ClientPortField.text;
|
||||||
int port = 3935;
|
int Port = 3935;
|
||||||
if (portText.Length > 0) {
|
if (PortText.Length > 0) {
|
||||||
port = Int32.Parse(portText);
|
Port = Int32.Parse(PortText);
|
||||||
}
|
}
|
||||||
StartClient(ip, port);
|
StartClient(IP, Port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartClient(string ip, int port) {
|
public void StartClient(string ip, int port) {
|
||||||
Client client = worldRoot.AddComponent<Client>();
|
Client Client = WorldRoot.AddComponent<Client>();
|
||||||
client.LaunchClient(ip, port);
|
Client.LaunchClient(ip, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartServer() {
|
public void StartServer() {
|
||||||
string portText = serverPortField.text;
|
string PortText = ServerPortField.text;
|
||||||
int port = 3935;
|
int Port = 3935;
|
||||||
if (portText.Length > 0) {
|
if (PortText.Length > 0) {
|
||||||
port = Int32.Parse(portText);
|
Port = Int32.Parse(PortText);
|
||||||
}
|
}
|
||||||
StartServer(port);
|
StartServer(Port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartServer(int port) {
|
public void StartServer(int port) {
|
||||||
Server server = worldRoot.AddComponent<Server>();
|
Server Server = WorldRoot.AddComponent<Server>();
|
||||||
server.LaunchServer(port);
|
Server.LaunchServer(port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: 430a933ca3f0fa849bc55f725fe9cffc
|
guid: 91755ff90ed584281ae73cf477fb817e
|
||||||
timeCreated: 1494190350
|
timeCreated: 1494205063
|
||||||
licenseType: Free
|
licenseType: Free
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
@ -16,10 +16,10 @@ public class Server : MonoBehaviour {
|
|||||||
|
|
||||||
public void LaunchServer(int port) {
|
public void LaunchServer(int port) {
|
||||||
|
|
||||||
ConnectionConfig config = new ConnectionConfig();
|
ConnectionConfig Config = new ConnectionConfig();
|
||||||
config.AddChannel(QosType.ReliableSequenced);
|
Config.AddChannel(QosType.ReliableSequenced);
|
||||||
config.AddChannel(QosType.UnreliableSequenced);
|
Config.AddChannel(QosType.UnreliableSequenced);
|
||||||
NetworkServer.Configure(config, 10);
|
NetworkServer.Configure(Config, 10);
|
||||||
|
|
||||||
NetworkServer.Listen(port);
|
NetworkServer.Listen(port);
|
||||||
|
|
||||||
@ -36,9 +36,9 @@ public class Server : MonoBehaviour {
|
|||||||
|
|
||||||
switch(msg.msgType) {
|
switch(msg.msgType) {
|
||||||
case PktType.TestMessage:
|
case PktType.TestMessage:
|
||||||
TextMessage textMsg = new TextMessage();
|
TextMessage TextMsg = new TextMessage();
|
||||||
textMsg.Deserialize(msg.reader);
|
TextMsg.Deserialize(msg.reader);
|
||||||
Debug.Log("Received message: " + textMsg.message);
|
Debug.Log("Received message: " + TextMsg.Message);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
||||||
|
Loading…
Reference in New Issue
Block a user