如何在lidgren 中发送数据类型消息?如果我理解正确的话,当你手动发送它时你会得到一个数据类型?即使我这样做了,它也不起作用......
我认为有点奇怪的是 client.ServerConnection 始终为空,即使我得到了一个连接......也许它可能是一些有用的信息。
这是代码:
class Program
{
static NetServer server;
static NetPeerConfiguration config;
static void Main(string[] args)
{
//Initialize configuration for server and start the server
config = new NetPeerConfiguration("chat");
config.Port = 14242;
config.LocalAddress = NetUtility.Resolve("localhost");
config.MaximumConnections = 10;
config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
server = new NetServer(config);
server.Start();
Console.WriteLine("Server is up and running!");
NetIncomingMessage inc;
Console.WriteLine("Waiting for connections...");
while (true)
{
if ((inc = server.ReadMessage()) != null)
{
switch (inc.MessageType)
{
case NetIncomingMessageType.ConnectionApproval:
Console.WriteLine("Incomnig LOGIN");
inc.SenderConnection.Approve();
NetOutgoingMessage outmsg = server.CreateMessage();
outmsg.Write("Welcome to my ChatRoom. Please behave, otherwise I'll eat you!");
outmsg.Write((byte)PacketType.MESSAGE);
server.SendMessage(outmsg, inc.SenderConnection, NetDeliveryMethod.ReliableOrdered, 0);
Console.WriteLine("Approved new connection.");
break;
case NetIncomingMessageType.Data:
outmsg = server.CreateMessage(inc.ReadString());
server.SendMessage(outmsg, server.Connections, NetDeliveryMethod.ReliableOrdered, 0);
break;
case NetIncomingMessageType.StatusChanged:
Console.WriteLine(inc.SenderConnection.ToString() + " Status Changed To: " + (NetConnectionStatus)inc.SenderConnection.Status);
break;
}
server.Recycle(inc);
}
}
}
}
}
class Program
{
static NetClient client;
static string hostip = "localhost";
static int port = 14242;
static bool isRunning = true;
static bool notConnected = true;
static void Main(string[] args)
{
NetPeerConfiguration config = new NetPeerConfiguration("chat");
client = new NetClient(config);
client.Start();
client.Connect("localhost", 14242);
Console.WriteLine("Client started. Checking for connection approval.");
if (client.ServerConnection != null)
CheckForConnetionApproval();
while (isRunning)
{
ReadMessages();
GetInputAndSendItToServer();
}
}
private static void ReadMessages()
{
NetIncomingMessage inc;
while ((inc = client.ReadMessage()) != null)
{
switch (inc.MessageType)
{
case NetIncomingMessageType.Data:
Console.WriteLine(inc.ReadString());
break;
}
}
}
private static void CheckForConnetionApproval()
{
bool CanStart = false;
NetIncomingMessage inc;
while (!CanStart)
{
if ((inc = client.ReadMessage()) != null)
{
switch (inc.MessageType)
{
case NetIncomingMessageType.Data:
Console.WriteLine(inc.ReadString());
CanStart = true;
break;
case NetIncomingMessageType.StatusChanged:
Console.WriteLine("Status: " + (NetConnectionStatus)inc.SenderConnection.Status);
break;
default:
//Console.WriteLine(inc.ReadString());
break;
}
}
}
}
static string text = "";
private static void GetInputAndSendItToServer()
{
ConsoleKeyInfo kinfo = Console.ReadKey();
while (kinfo.Key != ConsoleKey.Enter)
{
text = Console.ReadLine();
}
NetOutgoingMessage outmsg = client.CreateMessage(text);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered);
}
}