opt: close unused websocket connections

This commit is contained in:
RockYang
2024-04-30 22:54:39 +08:00
parent c0a7f41747
commit 3db55cbd48
17 changed files with 144 additions and 85 deletions

View File

@@ -204,6 +204,7 @@ func needLogin(c *gin.Context) bool {
c.Request.URL.Path == "/api/admin/login" ||
c.Request.URL.Path == "/api/admin/login/captcha" ||
c.Request.URL.Path == "/api/user/register" ||
c.Request.URL.Path == "/api/user/session" ||
c.Request.URL.Path == "/api/chat/history" ||
c.Request.URL.Path == "/api/chat/detail" ||
c.Request.URL.Path == "/api/chat/list" ||

View File

@@ -137,6 +137,7 @@ func (h *ChatHandler) ChatHandle(c *gin.Context) {
for {
_, msg, err := client.Receive()
if err != nil {
logger.Debugf("close connection: %s", client.Conn.RemoteAddr())
client.Close()
h.App.ChatClients.Delete(sessionId)
h.App.ChatSession.Delete(sessionId)

View File

@@ -6,10 +6,11 @@ import (
"chatplus/store"
"errors"
"fmt"
"github.com/imroc/req/v3"
"github.com/shirou/gopsutil/host"
"strings"
"time"
"github.com/imroc/req/v3"
"github.com/shirou/gopsutil/host"
)
type LicenseService struct {
@@ -20,7 +21,7 @@ type LicenseService struct {
machineId string
}
func NewLicenseService(server *core.AppServer, levelDB *store.LevelDB) * LicenseService {
func NewLicenseService(server *core.AppServer, levelDB *store.LevelDB) *LicenseService {
var license types.License
var machineId string
_ = levelDB.Get(types.LicenseKey, &license)
@@ -60,7 +61,7 @@ func (s *LicenseService) ActiveLicense(license string, machineId string) error {
}
if response.IsErrorState() {
return fmt.Errorf( "发送激活请求失败:%v", response.Status)
return fmt.Errorf("发送激活请求失败:%v", response.Status)
}
if res.Code != types.Success {
@@ -119,7 +120,7 @@ func (s *LicenseService) SyncLicense() {
IsActive: true,
}
s.urlWhiteList = res.Data.Urls
logger.Debugf("同步 License 成功:%v\n%v", s.license, s.urlWhiteList)
//logger.Debugf("同步 License 成功:%v\n%v", s.license, s.urlWhiteList)
next:
time.Sleep(time.Second * 10)
}
@@ -151,4 +152,4 @@ func (s *LicenseService) IsValidApiURL(uri string) error {
}
}
return fmt.Errorf("当前 API 地址 %s 不在白名单列表当中。", uri)
}
}