user.go
package main
import "net"
type User struct {
Name string
Addr string
C chan string
conn net.Conn
}
// 创建一个用户的API
func NewUser(conn net.Conn) *User {
userAddr := conn.RemoteAddr().String()
user := &User{
Name: userAddr,
Addr: userAddr,
C: make(chan string),
conn: conn,
}
// 启动监听当前user channel消息的goroutine
go user.ListenMessage()
return user
}
// 监听当前user channel的方法,一旦有消息就直接发送给客户端
func (this *User) ListenMessage() {
for {
msg := <-this.C
this.conn.Write([]byte(msg + "\n"))
}
}
server.go
package main
import (
"fmt"
"net"
"sync"
)
type Server struct {
Ip string
Port int
// 在线用户的列表
OnlineMap map[string]*User
mapLock sync.RWMutex
// 消息广播的channel
Message chan string
}
// 创建一个server的接口
func NewServer(ip string, port int) *Server {
server := &Server{
Ip: ip,
Port: port,
OnlineMap: make(map[string]*User),
Message: make(chan string),
}
return server
}
// 监听Message广播消息channel的goroutine,一旦有消息就发送给全部的在线User
func (this *Server) ListenMessager() {
for {
msg := <-this.Message
// 将message发送给全部的在线user
this.mapLock.Lock()
for _, cli := range this.OnlineMap {
cli.C <- msg
}
this.mapLock.Unlock()
}
}
// 广播消息的方法
func (this *Server) Broadcast(user *User, msg string) {
sendMsg := "[" + user.Addr + "]" + user.Name + ":" + msg
this.Message <- sendMsg
}
func (this *Server) Handler(conn net.Conn) {
// 当前连接的业务
fmt.Println("连接建立成功")
// 用户上线,将用户加入到OnlineMap中
user := NewUser(conn)
this.mapLock.Lock()
this.OnlineMap[user.Name] = user
this.mapLock.Unlock()
// 广播当前用户上线消息
this.Broadcast(user, "已上线")
// 当前hander阻塞
select {}
}
// 启动服务的接口
func (this *Server) Start() {
// socket listen
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", this.Ip, this.Port))
if err != nil {
fmt.Println("net.listen err:", err)
return
}
// close listen socket
defer listener.Close()
// 启动监听message的goroutine
go this.ListenMessager()
for {
// accept
conn, err := listener.Accept()
if err != nil {
fmt.Println("accept err:", err)
continue
}
// do handle
go this.Handler(conn)
}
}