lidgren 介绍和使用 (二)------集成unity测试
天冷,少打几个字吧,这次主要 以unity作为 客户端,同时 服务端和客户端 采用线程方式 接收和发送。不废话了,上代码。
服务端
class Program { private static NetServer s_server; private static Thread ReciveMsg; static void Main(string[] args) { NetPeerConfiguration config = new NetPeerConfiguration("chat"); config.MaximumConnections = 100; config.Port = 14242; s_server = new NetServer(config); s_server.Start(); ReciveMsg = new Thread(SocketReceive); ReciveMsg.Start(); } public static void SocketReceive() { while (true) { NetIncomingMessage msg; while ((msg = s_server.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); string reason = msg.ReadString(); Output(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + "===== " + status + ": " + reason); if (status == NetConnectionStatus.Connected) Output("Remote hail: " + msg.SenderConnection.RemoteHailMessage.ReadString()); UpdateConnectionsList(); break; case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.VerboseDebugMessage: string text = msg.ReadString(); Output(text); break; case NetIncomingMessageType.ErrorMessage: case NetIncomingMessageType.DebugMessage: var str = msg.ReadString(); if (str.StartsWith("Malformed packet; stated") || str.StartsWith("Received unhandled library message") || str.StartsWith("Unexpected NetMessageType")) break; // we'll get a bunch of these and we're fine with that Console.WriteLine(msg.MessageType + ": " + str); break; case NetIncomingMessageType.Data: string chat = msg.ReadString(); Output("Broadcasting '" + chat + "'"); List<NetConnection> all = s_server.Connections; // get copy all.Remove(msg.SenderConnection); if (all.Count > 0) { NetOutgoingMessage om = s_server.CreateMessage(); om.Write(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " said: " + chat); s_server.SendMessage(om, all, NetDeliveryMethod.ReliableOrdered, 0); } break; case NetIncomingMessageType.UnconnectedData: Console.WriteLine("Received " + msg.LengthBits + " bits of unconnected data"); break; default: Output("Unhandled type: " + msg.MessageType + " " + msg.LengthBytes + " bytes " + msg.DeliveryMethod + "|" + msg.SequenceChannel); break; } s_server.Recycle(msg); } Thread.Sleep(1); } } private static void Output(string text) { Console.WriteLine(text); } private static void UpdateConnectionsList() { foreach (NetConnection conn in s_server.Connections) { string str = NetUtility.ToHexString(conn.RemoteUniqueIdentifier) + " from " + conn.RemoteEndPoint.ToString() + " [" + conn.Status + "]"; Output(str); } } }
客户端
public class lidgrenClients : MonoBehaviour { private static Thread ReciveMsg; private static Thread SendMsg; private static NetClient s_client; public Text texts; void Start() { NetPeerConfiguration config = new NetPeerConfiguration("chat"); config.AutoFlushSendQueue = false; s_client = new NetClient(config); s_client.Shutdown("Bye"); s_client.Start(); NetOutgoingMessage hail = s_client.CreateMessage("This is the hail message"); s_client.Connect("192.168.1.101", 14242, hail); ReciveMsg = new Thread(SocketReceive); ReciveMsg.Start(); } void Update() { } public void Greet(string msg) { texts.text += msg + "\n"; NetOutgoingMessage om = s_client.CreateMessage(msg); s_client.SendMessage(om, NetDeliveryMethod.ReliableOrdered); s_client.FlushSendQueue();//排队 }
这是2个客户端 相互发送。
下一篇,开始 写 P2P 交互。大家期待