当前位置:网站首页>Unity多人联机框架Mirro学习记录(一)
Unity多人联机框架Mirro学习记录(一)
2022-07-29 08:06:00 【ToDoNothing】
NetworkServer中注册事件RegisterHandler
主要是注册服务端使用的事件
// message handlers
/// <summary>Register a handler for message type T. Most should require authentication.</summary>
// TODO obsolete this some day to always use the channelId version.
// all handlers in this version are wrapped with 1 extra action.
public static void RegisterHandler<T>(Action<NetworkConnectionToClient, T> handler, bool requireAuthentication = true)
where T : struct, NetworkMessage
{
ushort msgType = MessagePacking.GetId<T>();
if (handlers.ContainsKey(msgType))
{
Debug.LogWarning($"NetworkServer.RegisterHandler replacing handler for {
typeof(T).FullName}, id={
msgType}. If replacement is intentional, use ReplaceHandler instead to avoid this warning.");
}
handlers[msgType] = MessagePacking.WrapHandler(handler, requireAuthentication);
}
使用方法如下:
先设计传输的数据
public struct TestServerMessage: NetworkMessage
{
public string message;
}
在服务端注册事件,注意一定要在服务端,事件格式如下所示
NetworkServer.RegisterHandler<TestServerMessage>(OnTestServerMessage);
public void OnTestServerMessage(NetworkConnectionToClient conn, TestServerMessage msg)
{
Debug.Log(msg.message);
}
然后发送事件,客户端使用Send发送事件,发送给服务端执行,而不是发送给其他客户端
NetworkClient.Send(new TestServerMessage {
message="我来了"});
NetworkClient.Send() 函数
主要是发送数据给服务端,调用注册的服务端函数
// send
/// <summary>Send a NetworkMessage to the server over the given channel.</summary>
public static void Send<T>(T message, int channelId = Channels.Reliable)
where T : struct, NetworkMessage
{
if (connection != null)
{
if (connectState == ConnectState.Connected)
{
connection.Send(message, channelId);
}
else Debug.LogError("NetworkClient Send when not connected to a server");
}
else Debug.LogError("NetworkClient Send with no connection");
}
NetworkConnectionToClient中的Send函数
主要是向该连接的客户端发送消息,例如我在服务端保存了所有客户端NetworkConnectionToClient,可以通过该客户端的Send函数执行对应的事件
// Send stage one: NetworkMessage<T>
/// <summary>Send a NetworkMessage to this connection over the given channel.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Send<T>(T message, int channelId = Channels.Reliable)
where T : struct, NetworkMessage
{
using (NetworkWriterPooled writer = NetworkWriterPool.Get())
{
// pack message and send allocation free
MessagePacking.Pack(message, writer);
NetworkDiagnostics.OnSend(message, channelId, writer.Position, 1);
Send(writer.ToArraySegment(), channelId);
}
}
例如在服务器端运行的OnServerConnect函数中,检测到一旦有客户端加入,就会执行该函数,conn为加入函数的客户端NetworkConnectionToClient 。
public override void OnServerConnect(NetworkConnectionToClient conn){
}
创建一个客户端使用的传输数据
public struct TestClientMessage : NetworkMessage
{
public string message;
}
注册该事件,每个客户端都要注册,如果出现客户端没有注册该事件,但是仍然发送数据过去,就会出现下面这个错误,因为NetworkClient没有注册该事件,所以会出现该问题
一般注册在OnStartClient函数中
public override void OnStartClient(){
NetworkClient.RegisterHandler<TestClientMessage>(OnTestClientMessage);
}
public void OnTestClientMessage(TestClientMessage msg)
{
Debug.Log(msg.message);
}
注册完成之后,进行发送调用
public override void OnServerConnect(NetworkConnectionToClient conn){
conn.Send(new TestClientMessage {
message=conn.connectionId+"发来消息"});
}
边栏推荐
- 【学术相关】为什么很多国内学者的AI的论文复现不了?
- [beauty of software engineering - column notes] 30 | make good use of source code management tools to make your collaboration more efficient
- Up sampling deconvolution operation
- [beauty of software engineering - column notes] 28 | what is the core competitiveness of software engineers? (next)
- Crawl notes
- Alibaba political commissar system - Chapter 1: political commissars are built on companies
- Ionicons icon Encyclopedia
- How to draw an excellent architecture diagram
- [beauty of software engineering - column notes] 25 | what methods can improve development efficiency?
- 330. Complete the array as required
猜你喜欢
[introduction to cryoelectron microscopy] Caltech open class course notes part 3:image formation
Explanation and closing method of server 135, 137, 138, 139, 445 and other ports
Redshift 2.6.41 for maya2018 watermark removal
Unity beginner 4 - frame animation and protagonist attack (2D)
[beauty of software engineering - column notes] 22 | how to do a good job in technology selection for the project?
Mutationobserver document learning
Keyboard processing in jetpack compose
[cryoelectron microscope | paper reading] emclarity: software for high-resolution cryoelectron tomography and sub fault averaging
SQL 面试碰到的一个问题
Data unit: bit, byte, word, word length
随机推荐
Research on autojs wechat: the final product of wechat automatic information sending robot (effective demonstration)
网络安全之安全基线
[beauty of software engineering - column notes] 23 | Architect: programmers who don't want to be architects are not good programmers
在一个sql文件中,上面定义一个测试表及数据,下面可以select* from 测试表
Realize the effect of changing some colors of a paragraph of text
Greenplus enterprise deployment
Ue4/ue5 C disk enlargement processing
Simplefoc+platformio stepping on the path of the pit
Volatile keyword parsing of C #
Record of problems caused by PIP upgrade damage
Process and concept of process
STM32 serial port garbled
C language interview preparation I (about understanding Department)
Measured waveform of boot capacitor short circuit and open circuit of buck circuit
Unity - default rendering pipeline - sculpt shader
Solve the problem that CSDN cannot publish blog due to unknown copyright
(视频+图文)机器学习入门系列-第5章 机器学习实践
Unicode私人使用区域(Private Use Areas)
Tle5012b+stm32f103c8t6 (bluepill) reading angle data
Database persistence +jdbc database connection