http://blog.csdn.net/janeky/article/details/17233199
This is a simple unity project that implements the most basic chat room Group chat feature. After you log in to a chat room, users can enter information to speak and others can receive messages. The player can also change the nickname. The project details the process of the front-end socket communication, encoding, and parsing of the binary stream.
- The main objectives of the project are several:
1. Realizing Unity's front-end communication system
2. The application of learning Protobuf
A. Network communication NetClient.cs
TCP communication using C #. During initialization, the connection is established. Then there is no data at the end of each frame function update () Detection service on unity. In order to avoid sticky packets and other situations, the front and back end of the message body is such structure: the length of the message body + the content of the message body. In the process of parsing, the length of the message body is parsed, and then waits for sufficient binary stream, and finally decodes the entity object with Protobuf-net.
The process of sending a message is just the opposite. Serializes an entity object into a binary stream and then assembles it into a message body, sent to the backend
B.ui Design
The UI of the program is made using Ngui. The interface layout is relatively simple. Nickname box, message list, message box. The name is good, the layout is good, then the commit function is processed in the program, and the message is sent to the backend. When the back-end message is pushed over, it is displayed in the message list. There is no time to perfect the place, is currently not supported in Chinese, has not been timely fix font set.
The service side adopts Netty Network framework, which can realize a service side quickly and efficiently. In accordance with Netty's basic process, we can quickly build good one servers. The main task is to encode and decode the process. According to the message body structure above, we also parse the length first, then read the memory, and finally decode it with PROTOBUF. The server's business logic is simple, that is, the broadcast of the chat information. When the client connects, the connection is saved so that all connected broadcasts are made easy to follow.
The source of the project has been placed on GitHub, welcome to consult, suggestions. Have any questions welcome to explore [email protected]
Unity Hand Tour < three > chat room source based on Unity+java