代码拉取完成,页面将自动刷新
package hikvision
import (
"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/String"
"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/SyncMap"
"github.com/icholy/digest"
"net/http"
"strings"
"sync"
"time"
)
type clientManager struct {
clients sync.Map
}
func (e *clientManager) Register(ip string, port int, username, password string) {
host := "http://" + strings.ToLower(ip) + ":" + String.ToString(port)
options := make(map[string]interface{})
options["host"] = host
options["username"] = username
options["password"] = password
SyncMap.SetValue(&e.clients, host, "options", options)
}
func (e *clientManager) UnRegister(host string) {
host = strings.ToLower(host)
SyncMap.DelValue(&e.clients, host)
}
func (e *clientManager) GetClient(host string) (*http.Client, error) {
// 尝试取出client
client, err := SyncMap.GetValue(&e.clients, host, "client")
if err != nil {
return nil, err
}
// 该client存在,就返回该client
if client != nil {
client.(*http.Client).Timeout = 10 * time.Second
return client.(*http.Client), nil
}
// 重置一个Client
return e.ResetClient(host)
}
func (e *clientManager) ResetClient(host string) (*http.Client, error) {
// 不存在,就取出账号密码,生成一个新的client
options, err := SyncMap.GetValue(&e.clients, host, "options")
if err != nil {
return nil, err
}
client := &http.Client{
Transport: &digest.Transport{
Username: options.(map[string]interface{})["username"].(string),
Password: options.(map[string]interface{})["password"].(string),
},
}
SyncMap.SetValue(&e.clients, host, "client", client)
client.Timeout = 10 * time.Second
return client, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。