This is a creation in Article, where the information may have evolved or changed. [Zero] (Https://github.com/9b9387/zero) is a very lightweight socket server that I implemented with go, which can be used to quickly make a demo of the game, with no more than 500 lines of code throughout the project. Provides heartbeat detection, session management functions. The way to receive sending data using binary data streams can be easily used with protobuf. # # # message structure message structure definition in ' message.go ', the encoding and decoding operation of sending and receiving messages is handled in ' Codec.go '. The message structure is defined as follows: ' ' type message struct {msgsize int32//Message Length MsgId int32//Message Iddata []byte//Message data checksum UInt32//Check code ADLER32 Algorithm} "# # # Session Each connection corresponds to a Session object, created when the connection is established, and deleted when disconnected. Each session holds the current ' conn ' pointer and can bind a ' UserID '. The session also provides a key-value map for saving the customized information. # # # Connection When connection connection is established, the connection event is triggered. ' Conn.go ' implements the method of receiving and sending, the message is sent to Socketservice using the channel, and the ' OnMessage ' event is triggered, so the data is synchronized when the game logic is processed. Each connection reads a message, joins a time-out check to implement a heartbeat check, and, if no message is received within the set time, determines that the break event is triggered for heartbeat loss. # # # Socket Service Socket service is encapsulated within ' service.go '. You need to register an event to process the received message, the connection, the disconnection:-Regonmessagehandler (func (S *zero). Session, msg *zero. Message)-Regonconnecthandler (func (S *zero). Session)-Regondisconnecthandler (func (S *zero). Session, err Error)) When the service starts, initiates a accepthandler ' listening connection until the STOPCH receives the data and stops the service. A new ' Connecthandler ' is created to create the ' conn ' object and the ' session ' object after the connection is established。 When ' Conn ' is created, it initiates the process of receiving ' readcoroutine ' and sending ' writecoroutine '. ' Session ' will be saved in ' socketservice.sessions ' to be managed. * * Project Address **:[https://github.com/9b9387/zero] (Https://github.com/9b9387/zero) 590 reads
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.