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 realize instant messaging chat room with go language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to realize instant messaging chat room in go language". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to realize instant messaging chat room in go language" can help you solve your doubts.

Realized functions: public chat, private chat, modify user name

Only four classes are used:

Main.go: used to start the server

Server.go: server-related code

Client.go: client-side related code, a visual interface that users can manipulate directly

User.go: user class, which is used to encapsulate the user's business logic

Architecture diagram

Complete code

Server.go

Package mainimport ("fmt"io"net"sync"time") type Server struct {Ip string Port int / / online user list OnlineMap map [string] * User mapLock sync.RWMutex / / message broadcast Channel Message chan string} func NewServer (ip string Port int) * Server {server: = & Server {Ip: ip, Port: port, OnlineMap: make (map [string] * User), Message: make (chan string) } return server} func (s * Server) Handler (conn net.Conn) {/ / business logic / / fmt.Println ("link established successfully") user: = NewUser (conn, s) user.Online () / / monitor whether the user is active isLive: = make (chan bool) go func () {buf: = make ([] byte) 4096) for {n Error: = conn.Read (buf) if n = 0 {user.Offline () return} if error! = nil & & error! = io.EOF {fmt .Println ("read error")} msg: = string (buf [: NMel 1]) user.DoMessage (msg) / / indicates that the user is active isLive 7 & & msg [: 7] = "rename |" {/ / modify user name rename | xxx newName: = strings.Split (msg "|") [1] / / determine whether the name already exists _ Ok: = u.server.OnlineMap [newName] if ok {u.SendMsg ("user name already exists\ n")} else {u.server.mapLock.Lock () delete (u.server.OnlineMap U.Name) u.server.OnlineMap [newName] = u u.server.mapLock.Unlock () u.Name = newName u.SendMsg ("user name was successfully modified to: + newName +"\ n ")}} else if len (msg) > 4 & & msg [: 3] = "to |" {/ / Private chat to | zhangsan | Hello / / get the user name of the other party remoteName: = strings.Split (msg) "|") [1] if remoteName = "" {u.SendMsg ("user name format is incorrect\ n") return} / / get the other party's user remoteUser Ok: = u.server.OnlineMap [remoteName] if! ok {u.SendMsg ("user does not exist\ n") return} / / get message msg: = strings.Split (msg "|") [2] if msg = "" {u.SendMsg ("No message content) Resend\ n ")} / / send the message remoteUser.SendMsg (u.Name +" to you: "+ msg)} else {u.server.BroadCast (u, msg)}} func (u * User) ListenMessage () {for {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.

Share To

Development

Wechat

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

12
Report