Golang 生态拓展
1、Web 框架
.article-content a {color: #4ea1db;}.article-content{font-size:1rem !important}.article-content h3{font-size:1.125rem !important}
beego:https://github.com/asta……继续阅读 »
Alice
1年前 (2022-04-11) 364浏览 0评论
1个赞
版本九:客户端实现
client.go
1、客户端类型定义与连接
1)、定义一个 Client 类型
type Client struct {
ServerIp string
ServerPort int
Name string
conn net.Conn
}
2)、创建 client 对象,同时连接服务器
func NewCl……继续阅读 »
Alice
2年前 (2022-04-08) 431浏览 0评论
1个赞
版本七:超时强踢功能
⽤户的任意消息表示⽤户为活跃,⻓时间不发消息认为超时,就要强制关闭⽤户连接。
server.go
1)、在⽤户的 Hander() goroutine 中,添加⽤户活跃 channel,⼀旦有消息,就向该 channel 发送数据
//监听用户是否活跃的 channel
isLive := make(chan bool)
……继续阅读 »
Alice
2年前 (2022-04-07) 393浏览 0评论
1个赞
版本五:在线用户查询
规定一个指令,消息格式为”who“,一旦客户端输入这个指令,则查询当前在线用户列表
user.go
1)、提供 SendMsg 向对象客户端发送消息 API
//给当前 User 对应的客户端发送消息
func (this *User) SendMsg(msg string) {
this.conn.……继续阅读 »
Alice
2年前 (2022-04-07) 447浏览 0评论
1个赞
版本四:用户业务层封装
user.go
1)、user 类型新增 server 关联
type User struct {
Name string
Addr string
C chan string
conn net.Conn
server *Server
}
2)、新增 Online 方法
//用户的上线业务
……继续阅读 »
Alice
2年前 (2022-04-06) 446浏览 0评论
1个赞
版本三:用户消息广播机制
server.go
完善 handle 处理业务⽅法,启动⼀个针对当前客户端的读 goroutine
func (this *Server) Handler(conn net.Conn) {
//当前链接的业务
//fmt.Println("链接建立成功")
user := NewUser(c……继续阅读 »
Alice
2年前 (2022-04-06) 467浏览 0评论
1个赞
版本二:用户上线功能
结构图
user.go
1)、定义 User 类
type User struct {
Name string
Addr string
C chan string
conn net.Conn
}
2)、创建一个 user 对象
//创建一个用户的 API
func NewUser(conn net……继续阅读 »
Alice
2年前 (2022-04-05) 451浏览 0评论
1个赞
一、架构图
1、项目架构图
2、9 个小版本迭代
.article-content a{color:#4ea1db;}
二、版本一:构建基础 Server
server.go
1、定义 Server 类
type Server struct {
Ip string
Port int
}
2、创建一个 server 对象
//创建一个 ……继续阅读 »
Alice
2年前 (2022-04-05) 353浏览 0评论
1个赞
一、Go Modules
1、什么是 Go Modules
Go modules 是 Go 语⾔的依赖解决⽅案。
发布于 Go1.11,成⻓于 Go1.12,丰富于 Go1.13,正式于 Go1.14 推荐在⽣产上使⽤。
Go modules 目前集成在 Go 的工具链中,只要安装了 Go 就可以使用。
#sc_tips{background:none;b……继续阅读 »
Alice
2年前 (2022-04-04) 433浏览 0评论
1个赞
一、channel
1、channel 的定义
channel:管道
用于进程内各 goroutine 间通信。通过它可以在 goroutine 之间发送和接收消息。
它是 Golang 在语言层面提供的 goroutine 间的通信方式。
它的操作符是箭头 ……继续阅读 »
Alice
2年前 (2022-04-01) 325浏览 0评论
1个赞