增加IsConnect

main
wangbin 3 months ago
parent 08abef9b82
commit 073e743060

@ -148,6 +148,7 @@ func NewTcpServer() *TCPServer {
var CTcpServer *TCPServer
var AsClicent *tao.ClientConn
var reconnectInterval = 5 * time.Second
var IsConnect bool = false //是否连接服务器
func ConnectToServer() {
defer func() {
@ -186,15 +187,18 @@ func ConnectToServer() {
onError := tao.OnErrorOption(func(c tao.WriteCloser) {
holmes.Infoln("on error")
IsConnect = false
})
onClose := tao.OnCloseOption(func(c tao.WriteCloser) {
holmes.Infoln("连接关闭,尝试重连...")
IsConnect = false
go ConnectToServer() // 异步触发重连
})
onMessage := tao.OnMessageOption(func(msg tao.Message, c tao.WriteCloser) {
Remote_IP := strings.Split((c.(*tao.ClientConn)).Name(), ":")[0]
IsConnect = true
fmt.Println(msg.(CustomMessage).Content, Remote_IP)
})
@ -320,8 +324,8 @@ func (para *FLXNetworkCmdSender) SendToServer(dataMsg []byte) {
// fmt.Println("tcp发送失败", err.Error())
// }
// fmt.Println(dataMsg)
// SendLock.Lock()
if IsConnect {
SendLock.Lock()
// 伪代码示例:需确保遍历所有连接
@ -336,7 +340,15 @@ func (para *FLXNetworkCmdSender) SendToServer(dataMsg []byte) {
log.Error("SendToServer发送卡号失败:", msg)
}
// SendLock.Unlock()
SendLock.Unlock()
} else {
msg := CustomMessage{Content: dataMsg}
if !Common.Contain(UnSendToServerData, msg) {
UnSendToServerData = append(UnSendToServerData, msg)
}
log.Error("SendToServer发送卡号失败:", msg)
}
} else {
fmt.Printf("服务器没有连接!")

Loading…
Cancel
Save