我有这个代码来接收消息:
private void DataMessage(NetIncomingMessage msg)
{
NetOutgoingMessage outmsg = server.CreateMessage();
DataType data = (DataType)msg.ReadByte();
switch (data)
{
case DataType.DATATYPEMSG:
string s = msg.ReadString();
Console.WriteLine(s);
outmsg.Write(s);
outmsg.Write((byte)DataType.DATATYPEMSG);
server.SendToAll(outmsg,NetDeliveryMethod.ReliableUnordered);
break;
case DataType.DATATYPEOTHER:
outmsg.Write((byte)DataType.DATATYPEMSG);
outmsg.Write("please try this again later");
server.SendMessage(outmsg,msg.SenderConnection,NetDeliveryMethod.ReliableUnordered);
break;
case DataType.DATATYPEREQUEST:
break;
default: msg.SenderConnection.Disconnect("if you believe you where wrongly picked up for editing the code please contact the developer");
break;
}
}
以及发送它们的代码:
internal void sendmsg(string v)
{
NetOutgoingMessage outmsg = tclient.CreateMessage(v);
outmsg.Write((byte)DataType.DATATYPEMSG);
tclient.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered);
}
如果消息到达服务器,它只会调用默认值并断开用户连接。