Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize instant messaging chat room with go language

Shulou Source: shulou.com Published: 2022-05-31 17:29:34 09月29日 Update

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: =

Tags: User input user name content message server service online chat room language communication success code article link processing offline business customer client Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi Docker Huawei Apple