我是靠谱客的博主 积极柜子,最近开发中收集的这篇文章主要介绍Unity网络通信方式之Network,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

【消息管理】
public class CustomMessage : MessageBase {
public const short RecMsg = 1001;
public string msg = string.Empty;
public override void Serialize(NetworkWriter writer) {
writer.Write(msg);
}
public override void Deserialize(NetworkReader reader) {
msg = reader.ReadString();
}
}
【客户端】
public bool connected = false;
private NetworkClient client;
private string ipAddress = string.Empty;
private int port = 0;
private NetworkConnection netConn = null;
public void OpenClient() {
client.RegisterHandler(MsgType.Connect, OnConnectedServer);
client.RegisterHandler(MsgType.Disconnect, OnDisconnectedServer);
client.RegisterHandler(MsgType.Error, OnError);
client.RegisterHandler(CustomMessage.RecMsg, OnRecieveMessage);
client.Connect(ipAddress, port);
connected = true;
Debug.Log("Client opened.");
}
public void CloseClient() {
client.Disconnect();
connected = false;
Debug.Log("Client closed.");
}
public void OnSendMessage(string msg) {
CustomMessage customMessage = new CustomMessage();
customMessage.msg = msg;
netConn.Send(CustomMessage.RecMsg, customMessage);
}
private void OnRecieveMessage(NetworkMessage netMsg) {
CustomMessage customMessage = new CustomMessage();
customMessage.Deserialize(netMsg.reader);
if (customMessage.msg != null && customMessage.msg != string.Empty && customMessage.msg != message) {
message = customMessage.msg;
string prefix = message.Split(':')[0];
switch (prefix) {
case "AllowClientOnline": {
int.TryParse(message.Split(':')[1], out videoIndex);
BufferVideo();
long time = 0;
long.TryParse(message.Split(':')[3], out time);
videoPlayer.frame = time;
videoTimeSlider.value = time / videoPlayer.frameRate;
if (message.Split(':')[2] == "True") {
videoPlayer.Play();
}else if (message.Split(':')[2] == "False") {
videoPlayer.Pause();
}
}
break;
case "VideoChanged": {
int.TryParse(message.Split(':')[1], out videoIndex);
BufferVideo();
videoTimeSlider.value = 0;
}
break;
case "VideoStateChanged": {
if (message.Split(':')[1] == "True") {
videoPlayer.Play();
}else if (message.Split(':')[1] == "False") {
videoPlayer.Pause();
}
}
break;
}
}
}
private void OnConnectedServer(NetworkMessage netMsg) {
netConn = netMsg.conn;
connected = true;
Debug.Log("I am client , I connect server.");
}
private void OnDisconnectedServer(NetworkMessage netMsg) {
netConn = null;
connected = false;
Debug.Log("I am client , I disconnect server.");
}
private void OnError(NetworkMessage netMsg) {
connected = false;
Debug.Log("I am client , there is an error.");
}
【服务端】
private bool opened = false;
private string ipAddress = string.Empty;
private int port = 0;
private int maxConnectCount = 10;
private int connIndex = 1;
private Dictionary<int, NetworkConnection> netConns = new Dictionary<int, NetworkConnection>();
private int curConnectCount = 0;
private void OpenServer() {
opened = NetworkServer.Listen(ipAddress, port);
NetworkServer.RegisterHandler(MsgType.Error, OnError);
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnect);
NetworkServer.RegisterHandler(MsgType.Connect, OnConnect);
NetworkServer.RegisterHandler(CustomMessage.RecMsg, OnRecieveMessage);
}
private void CloseServer() {
NetworkServer.dontListen = true;
opened = false;
}
private void OnSendMessage(string msg) {
CustomMessage customMessage = new CustomMessage();
customMessage.msg = msg;
for (int i = 0; i < netConns.Count; i++) {
netConns[i].Send(CustomMessage.RecMsg, customMessage);
}
}
private void OnSendMessage(string msg, int id) {
CustomMessage customMessage = new CustomMessage();
customMessage.msg = msg;
NetworkServer.SendToClient(id, CustomMessage.RecMsg, customMessage);
}
private void OnRecieveMessage(NetworkMessage netMsg) {
if (!opened) {
return;
}
CustomMessage customMessage = new CustomMessage();
customMessage.Deserialize(netMsg.reader);
string prefix = customMessage.msg.Split(':')[0];
switch (prefix) {
//
//
}
}
private void OnConnect(NetworkMessage netMsg) {
if(connIndex == maxConnectCount) {
connIndex = 1;
}
NetworkConnection conn = netMsg.conn;
if (!netConns.ContainsKey(connIndex)) {
netConns.Add(connIndex, conn);
connIndex++;
Debug.Log("I am server , a client connected.");
}
}
private void OnDisconnect(NetworkMessage netMsg) {
NetworkConnection conn = netMsg.conn;
foreach(KeyValuePair<int,NetworkConnection>pair in netConns) {
if(pair.Value == conn) {
conn = null;
netConns.Remove(pair.Key);
Debug.Log("I am server , a client disconnected.");
}
}
}
private void OnError(NetworkMessage netMsg) {
opened = false;
Debug.Log("I am server , there is an error.");
}

最后

以上就是积极柜子为你收集整理的Unity网络通信方式之Network的全部内容,希望文章能够帮你解决Unity网络通信方式之Network所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部