服务端代码:
using LiteNetLib;
using LiteNetLib.Utils;
using System;
using System.Threading;
namespace LiteNetLibServer
{
class Program
{
static void Main(string[] args)
{
Manager manager = new Manager();
Console.ReadKey();
}
}
class Manager
{
public Manager()
{
EventBasedNetListener listener = new EventBasedNetListener();
NetManager server = new NetManager(listener);
server.Start(4096 /* port */);
Console.WriteLine("启动成功");
listener.ConnectionRequestEvent += request =>
{
if (server.PeersCount < 10 /* max connections */)
request.AcceptIfKey("SomeConnectionKey");
else
request.Reject();
};
listener.PeerConnectedEvent += peer =>
{
Console.WriteLine("We got connection: {0}", peer.EndPoint); // Show peer ip
NetDataWriter writer = new NetDataWriter(); // Create writer class
writer.Put((ushort)(1001));
writer.Put("Hello client!"); // Put some string
peer.Send(writer, DeliveryMethod.ReliableOrdered); // Send with reliability
};
listener.NetworkReceiveEvent += Listener_NetworkReceiveEvent;
while (!Console.KeyAvailable)
{
server.PollEvents();
Thread.Sleep(15);
}
server.Stop();
}
private void Listener_NetworkReceiveEvent(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
{
//throw new NotImplementedException();
ushort head = reader.GetUShort();
string msg = reader.GetString(100);
Console.WriteLine("recv: "+head+":"+msg);
NetDataWriter write = new NetDataWriter();
write.Put((ushort)1002);
write.Put("unity 我收到你发的消息了!!!");
peer.Send(write, DeliveryMethod.ReliableOrdered);
}
}
}
unity 代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LiteNetLib;
using System.Net;
using System.Net.Sockets;
using LiteNetLib.Utils;
public class LiteNetLibManager : MonoBehaviour,INetEventListener
{
private static LiteNetLibManager instance;
public static LiteNetLibManager Instance
{
get
{
return instance;
}
}
private void Awake()
{
instance = this;
}
public void OnConnectionRequest(ConnectionRequest request)
{
Debug.Log("OnConnectionRequest " + request.ToString());
}
public void OnNetworkError(IPEndPoint endPoint, SocketError socketError)
{
Debug.Log("OnNetworkError " + socketError.ToString());
}
public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
{
Debug.Log("OnNetworkLatencyUpdate " + peer.ConnectionState+": "+latency);
}
public void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
{
ushort head = reader.GetUShort();
string msg = reader.GetString(100);
Debug.Log("Recv " + head + ":" + msg);
reader.Recycle();
}
public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType)
{
//throw new System.NotImplementedException();
}
public void OnPeerConnected(NetPeer peer)
{
Debug.Log("OnPeerConnected " + peer.EndPoint.ToString());
}
public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
{
Debug.Log("OnPeerDisconnected " + disconnectInfo.ToString());
}
LiteNetLib.NetManager client;
void Start()
{
client = new LiteNetLib.NetManager(this);
client.Start();
client.Connect("127.0.0.1", 4096, "SomeConnectionKey");
}
public void SendMsg(ushort id,string msg)
{
if (client.ConnectedPeerList.Count < 1) return;
NetDataWriter w = new NetDataWriter();
w.Put(id);
w.Put(msg);
client.ConnectedPeerList[0].Send(w, DeliveryMethod.ReliableOrdered);
Debug.Log("ConnectedPeerList 的长度 :" + client.ConnectedPeerList.Count);
}
// Update is called once per frame
private void FixedUpdate()
{
client.PollEvents();
}
}
通过单例 LiteNetLibManager.Instance.SendMsg(id,msg)来发送消息