In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how GO uses socket and channel to achieve a simple console chat room. Many people may not know much about it. In order to make you understand better, the editor summarized the following content for you. I hope you can get something according to this article.
Use socket and channel to implement a simple console chat room
Here we use socket and channel to demonstrate how to write a simple network program in GO
Functional analysis
Main functions of chat room: users can join / leave chat room; messages sent by each user are broadcast to everyone
The chat room is divided into client and server. The client is responsible for sending messages and printing server messages, and the server is responsible for receiving client messages and broadcasting them to everyone.
Clients can use telnet programs
The server side needs to be implemented. Functions that need to be implemented
How to save the connection of multiple clients and manage the connection and disconnection
How to receive and broadcast client messages
Realization idea
Through functional analysis, it is divided into chat room structure and client structure.
The chat room structure is responsible for managing currently accessed clients and broadcast messages.
The client structure is responsible for managing socket connections and data that needs to be received and sent.
Notify the chat room when the client is connected / disconnected; the message sent by the client is actually forwarded to the chat room, which is then broadcast
Complete code package mainimport ("bufio"fmt"log"net") type Client struct {id string conn * net.Conn message chan string} type Hub struct {clients map [* Client] bool entering chan * Client leaving chan * Client messages chan string} func main () {hub: = & Hub { Clients: make (map [* Client] bool) Entering: make (chan * Client), leaving: make (chan * Client), messages: make (chan string),} listener, err: = net.Listen ("tcp") ": 8000") if err! = nil {log.Fatal (err)} go hub.broadcaster () for {conn Err: = listener.Accept () if err! = nil {log.Println (err) continue} go hub.handleConn (conn)}} func (hub * Hub) broadcaster () {for {select {case msg: =
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.