Introduction to the

Github address blog post

This article uses GO to implement a multiplayer Chat room, refer to the article Writing a Chat Server in Go, click to view the Chinese translation version.

The feature of this paper is that the original project is divided into several stages from bottom to top. The novice can realize different modules of the system step by step and gradually master the corresponding knowledge points. The source code has detailed comments to guide you to implement the code without looking at the source code.

This article assumes that you:

  • Understand the basic syntax of go
  • Learn how to use Git

By completing this project, you will have learned:

  • Basic use of the Reader method
  • usenetThe package implements a TCP server: listens for ports, establishes connections, and provides services
  • Sync lock
  • Goroutine and channel
  • Basic use of GOB

run

Operating environment: Go 1.13.1

  1. Open theGO111MODULERun,go mod downloadInstall dependencies
  2. Start the server:server/cmdRun under directorygo run main.go
  3. Start the client:tui/cmdRun under directorygo run main.go -server=localhost:3333, you can launch different clients in multiple Windows

Learning methods

This project has 5 branches, corresponding to 4 stages. The five branches are as follows:

  1. v0-template-code: template code that contains detailed comments but does not implement corresponding methods
  2. v1-protocol-reader-writer: realizes the protocol encoding and decoding based on string
  3. v2-server-implementation: Implements the server side
  4. v3-client-implementation: Implemented the client
  5. v4-gob-protocol: Uses GOB as the communication protocol

Corresponding stages are as follows:

  1. Implement the communication protocol based on string: V0 -> v1
  2. Implement server: v1 -> v2
  3. Implement client: v2 -> v3
  4. Use GOB as the communication protocol: V3 -> V4

It is recommended to implement each module in turn, starting with V0, based on the comments. If you’re really confused, check the code under the Master branch for hints.

Step profile

1. Implement the protocol

Run:

git checkout v0-template-code
Copy the code

In this step we will implement the communication protocol. TCP transmits unformatted byte streams. We need to define the format of these strings so that the corresponding commands and parameters can be parsed on the client and server.

The client and server transfer strings over TCP, so you need to specify a protocol for parsing strings into commands.

The format of a command is as follows:

[Command type] [Parameter 1] [Parameter 2]... [n] parameters \ nCopy the code

Each command ends with a newline character.

There are three kinds of commands:

  • NAME: Specifies the user name of the client
  • SEND: The client sends chat messages
  • MESSAGE: The server broadcasts chat messages to other users

For example, the client sends chat messages as follows:

	SEND somemessage\n
Copy the code

The command used by the server to broadcast messages to other users is:

	MESSAGE username somemessage\n
Copy the code

Protocol/command-go defines the command type. The corresponding methods in protocol/reader.go and protocol/writer.go need to be implemented.

2. Implement the server

Run:

git checkout v1-protocol-reader-writer
Copy the code

In this step we implement the server side. The server accepts the client’s connection request and saves all connections to send data to the client later.

The workflow of the server side is as follows:

  1. Listen: Starts a server and listens on a port
  2. Accept & Serve: Establish a connection with the client and Serve it
  3. Remove: Deletes the client’s connection after the client exits the connection
  4. Close: Stops the listening port and shuts down the server

The interactions between the server and the client are as follows:

  1. Receives messages from clients and broadcasts them to other clients
  2. Set the name of a client

Server /server.go defines the behavior of the server as an interface, and server/tcp_server.go is the corresponding implementation.

Implementing the client

Run:

git checkout v2-server-implementation
Copy the code

In this step we will implement the client. In fact, the client is relatively simple, just need to connect to the server, and then send messages to the server or receive messages from the server. This part of the work is mainly UI implementation, but I didn’t isolate it. Interested readers can check out the source code under tuI /.

Use GOB as the communication protocol

Run:

git checkout v3-client-implementation
Copy the code

In this step we will use GOB as the communication protocol instead of the string-based protocol. You only need to modify the Read() and Write() methods of protocol/reader.go and protocol/writer.go, involving two sets of goB methods: NewEncoder/Encoder and NewDecoder/Decode. Isn’t it convenient?

Next step optimization

This project can be further optimized, interested readers are welcome to submit your PR!

  1. Use RPC calls instead of TCP calls, such as GRPC
  2. Data persistence: The chat records can be seen by people who join the chat room later. Data storage can be goB coded
  3. The server supports multiple chat rooms
  4. Advanced content: support disconnection reconnection, you can check this article

References:

  • Writing a Chat Server in Go
  • Write a simple chat room with Golang