0

我有这个代码来接收消息:

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);
}

如果消息到达服务器,它只会调用默认值并断开用户连接。

4

2 回答 2

0

创建消息时,您在 DataType 之前编写消息文本,但是当您收到消息时,您希望 DataType 出现在文本之前。

您可以通过在文本前写入 DataType 来修复它,如下所示:

internal void sendmsg(string v)
{
    NetOutgoingMessage outmsg = tclient.CreateMessage();
    outmsg.Write((byte)DataType.DATATYPEMSG);
    outmsg.Write(v);
    tclient.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered);
}
于 2018-08-27T14:43:27.753 回答
0

我改为 NetOutgoingMessage outmsg = server.CreateMessage(); DataType data = (DataType)msg.ReadByte(); switch (data) { case DataType.DATATYPEMSG: string s = msg.ReadString(); NetOutgoingMessage outmsg = server.CreateMessage(); DataType data = (DataType)msg.ReadByte(); string s = msg.ReadString();

于 2018-08-27T20:36:44.280 回答