2.Communication between client and server
In the last article, we learned the use of the ET framework, Now we learn communication between client and server.
ET comunication use of the protobuf
open ET\Proto folder, You can see three proto file
InnerMessage: This proto file is used to write the communication message between the server and the server.
OuterMessage: This proto file is used to write the communication message between the client and the server.
MongoMessage: communication message between the client and the server but can write Entity.
Now, Open ET\Proto directory with VS Code, select OuterMessage.proto file, input:
C-Client、2-To、S-Server, Equal to ClientToServer, C2S_TestRequest is request message, S2C_TestResponse is response message, if is rquest message then must have response message.
Tips: request message must have RpcId and value must equal to 90. Response message must have RpcId、Error、Message.And value must namely:90、91、92.
Now, Back to ET\Proto\, Mouse double-click win_startProtoExport.bat file.
Because I don’t have a Mac Computer, So I can‘t demonstrate the operation under the Mac system
If console print “proto2cs succeed” then congratulations.
Now, open Client-Server.sln file in ET folder under and find LoginHelper.cs file, new a LoginTest method in Login method under
First declare a response message variable and a session, Then use session from client send a message to server, Now, new a folder in Server\Server.Hotfix\Demo\ under, folder name is “Test”, create a class file in Test folder under after completed, file name is “C2S_TestRequestHandler.cs”
This class is a MessageHandler file, So need inherit AMRpcHandler and override Run method. After input response message, use reply() method send message to client. Because codes not await, So need write await ETTask.Completed,Else will error.
And…this class namespace must is ET
Now!!!!Find where the Login method is called and change to LoginTest method:
Save all codes, Back to Unity Edit interface find Tools →Build →BuildCodeDebug, Mouse click BuildCodeDebug, If Unity console print build success! then ok
Back to Client-Server.sln run Server.App and run Unity game:
Mouse click “登录”, You can in Unity console see a message, We successfully received the message from the Server
Now, See if the Server received the message from the Client, Enter into ET\Logs\ directory find Server.xxxxx.xxxxxxx.Debug.Log file and open that,Look that!!!Server successfully received the message from the Client
All right, Today learning Over!!