实现 keepalive
This commit is contained in:
parent
59fb38eff8
commit
5f07f5eac7
@ -2,8 +2,10 @@ package main
|
||||
|
||||
import (
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/config"
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/handler/keepalive"
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/handler/message"
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/log"
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/services/device"
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/services/zlmediakit"
|
||||
"github.com/emiago/sipgo"
|
||||
"github.com/emiago/sipgo/sip"
|
||||
@ -59,6 +61,9 @@ func main() {
|
||||
srv, _ := sipgo.NewServer(ua, sipgo.WithServerLogger(logger))
|
||||
|
||||
message.SetupMessageHandler(srv, client, clientConfig)
|
||||
keepalive.SetupKeepalive(client, clientConfig)
|
||||
device.StartKeepAlive(client)
|
||||
defer device.StopKeepAlive()
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
go func() {
|
||||
|
30
pkg/handler/keepalive/keepalive.go
Normal file
30
pkg/handler/keepalive/keepalive.go
Normal file
@ -0,0 +1,30 @@
|
||||
package keepalive
|
||||
|
||||
import (
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/config"
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/manscdp"
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/services/device"
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/utils"
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/utils/charset"
|
||||
"github.com/duke-git/lancet/v2/random"
|
||||
"github.com/emiago/sipgo"
|
||||
"github.com/emiago/sipgo/sip"
|
||||
)
|
||||
|
||||
func SetupKeepalive(client *sipgo.Client, clientConfig *config.ClientConfig) {
|
||||
device.AddKeepaliveSender(clientConfig.DeviceId, func(client *sipgo.Client) {
|
||||
sn := random.RandNumeral(6)
|
||||
data := manscdp.NewKeepAliveReqWithOK(sn, clientConfig.DeviceId)
|
||||
target := sip.Uri{
|
||||
User: clientConfig.ServerId,
|
||||
Host: clientConfig.ServerIp,
|
||||
Port: clientConfig.ServerPort,
|
||||
Headers: sip.NewParams(),
|
||||
}
|
||||
|
||||
req := sip.NewRequest(sip.MESSAGE, target)
|
||||
marshal, _ := utils.XMLMarshal(data, charset.GBK)
|
||||
req.SetBody(marshal)
|
||||
_ = client.WriteRequest(req)
|
||||
})
|
||||
}
|
@ -1 +1,34 @@
|
||||
package manscdp
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/manscdp/cmdtype"
|
||||
)
|
||||
|
||||
type KeepAliveReq struct {
|
||||
XMLName xml.Name `xml:"Notify"`
|
||||
CmdType string `xml:"CmdType"`
|
||||
SN string `xml:"SN"`
|
||||
DeviceID string `xml:"DeviceID"`
|
||||
Status string `xml:"Status"`
|
||||
}
|
||||
|
||||
func NewKeepAliveReqWithOK(sn, deviceID string) *KeepAliveReq {
|
||||
return &KeepAliveReq{
|
||||
XMLName: xml.Name{Local: "Notify"},
|
||||
CmdType: cmdtype.Keepalive,
|
||||
SN: sn,
|
||||
DeviceID: deviceID,
|
||||
Status: "OK",
|
||||
}
|
||||
}
|
||||
|
||||
func NewKeepAliveReq(sn, deviceID, status string) *KeepAliveReq {
|
||||
return &KeepAliveReq{
|
||||
XMLName: xml.Name{Local: "Notify"},
|
||||
CmdType: cmdtype.Keepalive,
|
||||
SN: sn,
|
||||
DeviceID: deviceID,
|
||||
Status: status,
|
||||
}
|
||||
}
|
||||
|
38
pkg/services/device/keepalive.go
Normal file
38
pkg/services/device/keepalive.go
Normal file
@ -0,0 +1,38 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"git.skcks.cn/Shikong/go-gb28181/pkg/log"
|
||||
"github.com/emiago/sipgo"
|
||||
"time"
|
||||
)
|
||||
|
||||
var keepaliveTimer *time.Ticker
|
||||
|
||||
type SendKeepAlive = func(client *sipgo.Client)
|
||||
|
||||
var senders = make(map[string]SendKeepAlive)
|
||||
|
||||
func AddKeepaliveSender(deviceId string, sender SendKeepAlive) {
|
||||
log.Log().Info().Msgf("添加 keealive 发送器, deviceId: %s", deviceId)
|
||||
senders[deviceId] = sender
|
||||
}
|
||||
|
||||
// StartKeepAlive 启动 keepalive 定时器
|
||||
// 每 30 秒发送一次 keepalive 消息到服务器
|
||||
func StartKeepAlive(client *sipgo.Client) {
|
||||
keepaliveTimer = time.NewTicker(time.Second * 30)
|
||||
go func() {
|
||||
for range keepaliveTimer.C {
|
||||
// 遍历所有注册的设备,发送 keepalive 消息
|
||||
for deviceId, sender := range senders {
|
||||
log.Log().Debug().Msgf("执行 发送 keealive 消息, deviceId: %s", deviceId)
|
||||
go sender(client)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// StopKeepAlive 停止 keepalive 定时器
|
||||
func StopKeepAlive() {
|
||||
keepaliveTimer.Stop()
|
||||
}
|
Loading…
Reference in New Issue
Block a user