Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use go to realize multi-person chat room

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use go to achieve a multi-person chat room". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Project presentation

We can clone the project locally on github and enter the following command:

Need to pay attention to the installation of bee tools may have problems, here you need to test, enter bee in the terminal, if there is no problem, if not, then install the bee tool error, you can see my blog (https://blog.csdn.net/qq_39397165/article/details/106406773) to install.

After the environment is installed, we can run the project. Enter bee run under the terminal to run the project. The successful operation is shown in the following figure:

Open the browser and enter localhost:8080, and you can see the following interface:

No account we can register first, click the registration button, you can enter the registration interface, enter the relevant information to complete the registration.

Then enter your mobile phone number and password to enter the chat room, and the two can chat in the chat room, as shown in the screenshot below:

We can see that the chat transcript is described in detail, is such a simple project, you can quickly familiarize yourself with the beego framework. The following is to introduce some knowledge points.

WebSocket

Since we want to be a chat room, we need to communicate in real time, and there are many options here:

Polling: the client sends Ajax requests at regular intervals, which is easy to implement, but consumes bandwidth and server resources, and requires frequent establishment and release of TCP connections.

Long polling: unlike normal polling, the server will not return a response immediately after receiving the request, and will not respond and close the connection until there is a message update, and the client will re-initiate the request after processing the response. Compared with ordinary polling, there are no useless requests, but it is also costly for the server to maintain a connection, which is no different from ordinary polling if the server data changes frequently.

Persistent connection: embed a hidden iframe in the page and set its src to a long-connected request, so that the server can constantly send data to the client, with advantages and disadvantages similar to long polling.

These technologies all have great shortcomings, resulting in high server overhead, so I finally chose websocket, which only needs a handshake to complete the connection between the client and the service, and then communicates on this connection through data frames. The communication is also two-way, and the server can also actively send messages to the client, which reduces a lot of business logic. Coding has also become easier.

It is also easy for Golang to use websocket, just import the package "github.com/gorilla/websocket" and we can use it. Specific use will not be introduced here, let's take a look at the code example of this project:

Beego

Here I am using the beego framework, this framework is very simple to use, after all, is developed by Chinese, there are complete Chinese documents, according to the document we can quickly reach the entry level. Beego is a HTTP framework for rapid development of Go applications. It can be used to quickly develop various applications such as API, WEB and back-end services. It is a RESTful framework that combines some characteristics of Go itself to design a framework. Specific study of this framework we go to (https://beego.me/quickstart) this website to learn, which has a detailed introduction. It's good to be an entry-level framework.

Here, I use bee, a tool of the framework, to create a web project. After the project is created, the entire project structure is available. The project structure is as follows:

From the directory structure, we can see that conf is used to place configuration files. The three directories models, views and controllers conform to the MVC architecture. Main.go is the portal file and the routers directory is used to configure routing. This is the whole project structure. Is it very simple?

Mysql

The Mysql database is used here. Only two tables have been created, one user table and one message table. The user table is used to store user information, and the message table is used to store message records. Because it is a hands-on project, there is no design of more tables, only at the entry level. The table structure is as follows:

Here is an introduction to beego ORM. Beego ORM is a powerful Go language ORM framework. There are three database drivers supported: Mysql, PostgreSQL, and Sqlite3, where Mysql is used. Enter the following command to complete the installation:

Go get github.com/astaxie/beego/orm

After the installation is complete, we start to use it. Because the Golang language has an init function, we can register the database driver and register an alias database into the init function to complete the database driver registration. The code example is as follows:

Here we use the RunSyndb method, with which we do not have to create the database table manually, but actively create it according to the table structure in the structure, which improves efficiency.

There is also a point to pay attention to, such as we need our project is more complex, need high-level query, it is necessary to register the model, otherwise it will not be available. Model registration can be completed using RegisterModel. Examples of use are as follows:

This is the end of the content of "how to use go to implement a multi-person chat room". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report