Skip to content

Commit

Permalink
定时清理不活跃连接
Browse files Browse the repository at this point in the history
添加页面模板
  • Loading branch information
link1st committed Jul 31, 2019
1 parent 7eab36a commit ffe8110
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 25 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ govendor add -tree github.com/spf13/viper
```

#### 待办事项
- gin log日志(请求日志+debug日志)
- 读取配置文件 完成
- 定时脚本,清理过期未心跳链接
- 定时脚本,清理过期未心跳链接 完成
- http接口,获取登录、链接数量 完成
- http接口,发送push、查询有多少人在线 完成
- grpc 程序内部通讯,发送消息
- appIds 一个用户在多个平台登录
- 界面,把所有在线的人拉倒一个群里面,发送消息
- ~~群聊~~单聊
- ~~单聊~~群聊
- 实现分布式,水平扩张
21 changes: 21 additions & 0 deletions controllers/home/home_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Created by GoLand.
* User: link1st
* Date: 2019-07-25
* Time: 12:11
*/

package home

import (
"github.com/gin-gonic/gin"
"net/http"
)

// 查看用户是否在线
func Index(c *gin.Context) {
data := gin.H{
"title": "聊天首页",
}
c.HTML(http.StatusOK, "index.tpl", data)
}
15 changes: 0 additions & 15 deletions controllers/test/test_controller.go

This file was deleted.

4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/viper"
"gowebsocket/lib/redislib"
"gowebsocket/routers"
"gowebsocket/servers/task"
"gowebsocket/servers/websocket"
"io"
"net/http"
Expand All @@ -31,6 +32,9 @@ func main() {
routers.Init(router)
routers.WebsocketInit()

// 定时任务
task.Init()

go websocket.StartWebSocket()

httpPort := viper.GetString("app.httpPort")
Expand Down
12 changes: 10 additions & 2 deletions routers/web_routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ package routers

import (
"github.com/gin-gonic/gin"
"gowebsocket/controllers/home"
"gowebsocket/controllers/systems"
"gowebsocket/controllers/user"
)

func Init(router *gin.Engine) {
router.LoadHTMLGlob("views/**/*")

// 用户组
userRouter := router.Group("/user")
Expand All @@ -24,9 +26,15 @@ func Init(router *gin.Engine) {
}

// 系统
system := router.Group("/system")
systemRouter := router.Group("/system")
{
system.GET("/state", systems.Status)
systemRouter.GET("/state", systems.Status)
}

// home
homeRouter := router.Group("/home")
{
homeRouter.GET("/index", home.Index)
}

// router.POST("/user/online", user.Online)
Expand Down
37 changes: 37 additions & 0 deletions servers/task/clean_connection _task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Created by GoLand.
* User: link1st
* Date: 2019-07-31
* Time: 15:17
*/

package task

import (
"fmt"
"gowebsocket/servers/websocket"
"runtime/debug"
"time"
)

func Init() {
Timer(3*time.Second, 30*time.Second, cleanConnection, "")

}

// 清理超时连接
func cleanConnection(param interface{}) (result bool) {
result = true

defer func() {
if r := recover(); r != nil {
fmt.Println("ClearTimeoutConnections stop", r, string(debug.Stack()))
}
}()

fmt.Println("定时任务,清理超时连接", param)

websocket.ClearTimeoutConnections()

return
}
24 changes: 21 additions & 3 deletions servers/websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"runtime/debug"
)

const (
// 用户连接超时时间
heartbeatExpirationTime = 6 * 60
)

// 用户登录
type Login struct {
AppId uint32
Expand Down Expand Up @@ -64,10 +69,14 @@ func (c *Client) read() {
defer func() {
if r := recover(); r != nil {
fmt.Println("write stop", string(debug.Stack()), r)

}
}()

defer func() {
fmt.Println("读取客户端数据 关闭send", c)
close(c.Send)
}()

for {
_, message, err := c.Socket.ReadMessage()
if err != nil {
Expand All @@ -77,12 +86,12 @@ func (c *Client) read() {
}

// 处理程序
fmt.Println("读取客户端数据 处理", string(message))
fmt.Println("读取客户端数据 处理:", string(message))
ProcessData(c, message)
}
}

// 读取客户端数据
// 向客户端写数据
func (c *Client) write() {
defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -138,6 +147,15 @@ func (c *Client) Heartbeat(currentTime uint64) {
return
}

// 心跳超时
func (c *Client) IsHeartbeatTimeout(currentTime uint64) (timeout bool) {
if c.HeartbeatTime+heartbeatExpirationTime <= currentTime {
timeout = true
}

return
}

// 是否登录了
func (c *Client) IsLogin() (isLogin bool) {

Expand Down
18 changes: 15 additions & 3 deletions servers/websocket/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"gowebsocket/lib/cache"
"sync"
"time"
)

// 连接管理
Expand Down Expand Up @@ -139,9 +140,6 @@ func (manager *ClientManager) EventUnregister(client *Client) {
if err == nil {
userOnline.LogOut()
cache.SetUserOnlineInfo(client.GetKey(), userOnline)

// 退出登录事件
// currentTime := uint64(time.Now().Unix())
}

// 关闭 chan
Expand Down Expand Up @@ -215,3 +213,17 @@ func GetUserClient(appId uint32, userId string) (client *Client) {

return
}

// 定时清理超时连接
func ClearTimeoutConnections() {

currentTime := uint64(time.Now().Unix())

for client := range clientManager.Clients {
if client.IsHeartbeatTimeout(currentTime) {
fmt.Println("心跳时间超时 关闭连接", client.Addr, client.UserId, client.LoginTime, client.HeartbeatTime)

client.Socket.Close()
}
}
}
9 changes: 9 additions & 0 deletions views/home/index.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>{{ .title }}</title>
</head>
<body>

</body>
</html>

0 comments on commit ffe8110

Please sign in to comment.