当前位置:网站首页>Unity multiplayer online framework mirror learning record (I)
Unity multiplayer online framework mirror learning record (I)
2022-07-29 08:06:00 【ToDoNothing】
NetworkServer Registered events in RegisterHandler
It is mainly the event used by the registration server
// 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);
}
How to use it is as follows :
First design the transmitted data
public struct TestServerMessage: NetworkMessage
{
public string message;
}
stay Server side Registration events , Pay attention to the server , The event format is as follows
NetworkServer.RegisterHandler<TestServerMessage>(OnTestServerMessage);
public void OnTestServerMessage(NetworkConnectionToClient conn, TestServerMessage msg)
{
Debug.Log(msg.message);
}
Then send the event , Client side usage Send Send events , Send it to the server for execution , Instead of sending it to other clients
NetworkClient.Send(new TestServerMessage {
message=" I'm coming "});
NetworkClient.Send() function
It mainly sends data to the server , Call the registered server function
// 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 Medium Send function
It mainly sends messages to the client of the connection , For example, I saved all clients on the server NetworkConnectionToClient, You can use the Send Function executes the corresponding event
// 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);
}
}
For example, running on the server OnServerConnect Function , It is detected that once a client joins , Will execute the function ,conn For clients that add functions NetworkConnectionToClient .
public override void OnServerConnect(NetworkConnectionToClient conn){
}
Create a transmission data used by the client
public struct TestClientMessage : NetworkMessage
{
public string message;
}
Register this event , Every client should register , If the client does not register this event , But still send data in the past , The following error will appear , because NetworkClient This event is not registered , So there will be this problem 
Generally registered in OnStartClient Function
public override void OnStartClient(){
NetworkClient.RegisterHandler<TestClientMessage>(OnTestClientMessage);
}
public void OnTestClientMessage(TestClientMessage msg)
{
Debug.Log(msg.message);
}
After registration , Make a send call
public override void OnServerConnect(NetworkConnectionToClient conn){
conn.Send(new TestClientMessage {
message=conn.connectionId+" Send a message "});
}
边栏推荐
- Shell script - global variables, local variables, environment variables
- MySQL rownum implementation
- [memo] summary of the reasons why SSH failed? Remember to come next time.
- [beauty of software engineering - column notes] 22 | how to do a good job in technology selection for the project?
- [beauty of software engineering - column notes] "one question and one answer" issue 2 | 30 common software development problem-solving strategies
- C language problems
- [skill accumulation] common expressions when writing emails
- Detailed explanation of two modes of FTP
- UE4 highlight official reference value
- [lecture notes] how to do in-depth learning in poor data?
猜你喜欢

Jiamusi Market Supervision Bureau carried out special food safety network training on epidemic and insect prevention

Measured waveform of boot capacitor short circuit and open circuit of buck circuit
![[paper reading] tomoalign: a novel approach to correcting sample motion and 3D CTF in cryoet](/img/3a/75c211f21758ca2d9bb1a40d739d80.png)
[paper reading] tomoalign: a novel approach to correcting sample motion and 3D CTF in cryoet

Explanation and closing method of server 135, 137, 138, 139, 445 and other ports

Amaze UI icon query
![[cryoelectron microscope | paper reading] interpretation of sub fault average m software: multi particle cryo EM refining with M](/img/5e/69987afcd1e50ba37bc49441dd3a50.png)
[cryoelectron microscope | paper reading] interpretation of sub fault average m software: multi particle cryo EM refining with M
![[beauty of software engineering - column notes]](/img/90/658f4d954c876bed2fccb431232e52.png)
[beauty of software engineering - column notes] "one question and one answer" issue 2 | 30 common software development problem-solving strategies
![[lecture notes] how to do in-depth learning in poor data?](/img/7d/5767c078600bd88b7d2146069f4f40.jpg)
[lecture notes] how to do in-depth learning in poor data?

Compare three clock circuit schemes of single chip microcomputer

Day 014 二维数组练习
随机推荐
Operator overloading
[beauty of software engineering - column notes] "one question and one answer" issue 2 | 30 common software development problem-solving strategies
postman接口测试|js脚本之阻塞休眠和非阻塞休眠
[beauty of software engineering - column notes] 26 | continuous delivery: how to release new versions to the production environment at any time?
sql判断语句的编写
10 common software architecture modes
Exercise: store department information
torch.nn.functional.one_hot()
Simplefoc parameter adjustment 2- speed and position control
Shell script - global variables, local variables, environment variables
Autojs微信研究:微信自动发送信息机器人最终成品(有效果演示)
在一个sql文件中,上面定义一个测试表及数据,下面可以select* from 测试表
C language problems
Useful websites
SQL 面试碰到的一个问题
[beauty of software engineering - column notes] 25 | what methods can improve development efficiency?
Explanation and closing method of server 135, 137, 138, 139, 445 and other ports
Simplefoc+platformio stepping on the path of the pit
Crawl expression bag
Matrix decomposition and gradient descent