Added a small fun chat command
This commit is contained in:
parent
117f2ddfff
commit
3bd2e17ab8
@ -33,6 +33,8 @@ public class Client : MonoBehaviour {
|
|||||||
|
|
||||||
Running = true;
|
Running = true;
|
||||||
|
|
||||||
|
NetClient.RegisterHandler(PktType.TextMessage, HandlePacket);
|
||||||
|
|
||||||
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
|
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
|
||||||
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
||||||
NetClient.RegisterHandler(MsgType.Error, OnError);
|
NetClient.RegisterHandler(MsgType.Error, OnError);
|
||||||
@ -43,10 +45,32 @@ public class Client : MonoBehaviour {
|
|||||||
Term.Println("Client launched!");
|
Term.Println("Client launched!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
public void OnConnected(NetworkMessage msg) {
|
public void OnConnected(NetworkMessage msg) {
|
||||||
Debug.Log("Connected!");
|
Debug.Log("Connected!");
|
||||||
Term.Println("Connected!");
|
Term.Println("Connected!");
|
||||||
NetClient.Send(PktType.TestMessage, new TextMessage("Hai, I connected!"));
|
|
||||||
|
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]));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnDisconnected(NetworkMessage msg) {
|
public void OnDisconnected(NetworkMessage msg) {
|
||||||
|
@ -4,5 +4,5 @@ using UnityEngine;
|
|||||||
|
|
||||||
public class PktType {
|
public class PktType {
|
||||||
|
|
||||||
public const short TestMessage = 200;
|
public const short TextMessage = 200;
|
||||||
}
|
}
|
@ -23,7 +23,7 @@ public class Server : MonoBehaviour {
|
|||||||
|
|
||||||
NetworkServer.Listen(port);
|
NetworkServer.Listen(port);
|
||||||
|
|
||||||
NetworkServer.RegisterHandler(PktType.TestMessage, HandlePacket);
|
NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket);
|
||||||
|
|
||||||
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
|
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
|
||||||
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
||||||
@ -31,23 +31,30 @@ public class Server : MonoBehaviour {
|
|||||||
|
|
||||||
Debug.Log("Server started on port " + port);
|
Debug.Log("Server started on port " + port);
|
||||||
Term.Println("Server started on port " + port);
|
Term.Println("Server started on port " + port);
|
||||||
|
|
||||||
|
Term.AddCommand("send (message)", "Howl at the darkness of space. Does it echo though?", (args) => {
|
||||||
|
Term.Println("You: " + args[0]);
|
||||||
|
SendToAll(PktType.TextMessage, new TextMessage("Server: " + args[0]));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void HandlePacket(NetworkMessage msg) {
|
public void HandlePacket(NetworkMessage msg) {
|
||||||
|
|
||||||
switch(msg.msgType) {
|
switch(msg.msgType) {
|
||||||
case PktType.TestMessage:
|
case PktType.TextMessage:
|
||||||
TextMessage TextMsg = new TextMessage();
|
TextMessage TextMsg = new TextMessage();
|
||||||
TextMsg.Deserialize(msg.reader);
|
TextMsg.Deserialize(msg.reader);
|
||||||
Debug.Log("Received message: " + TextMsg.Message);
|
Term.Println(TextMsg.Message);
|
||||||
Term.Println("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);
|
||||||
Term.Println("Received an unknown packet, id: " + msg.msgType);
|
Term.Println("Received an unknown packet, id: " + msg.msgType);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SendToAll(short type, MessageBase message) {
|
||||||
|
NetworkServer.SendToAll(type, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnConnected(NetworkMessage msg) {
|
public void OnConnected(NetworkMessage msg) {
|
||||||
|
Loading…
Reference in New Issue
Block a user