The video intelligent analysis platform EasyCVR establishes a connection with the third-party platform through websocket and the network is disconnected and cannot be connected again. How to fix it?

In many cases, the commonly used protocol is the http protocol, but http has an obvious shortcoming: communication can only be initiated by the client, so when compiling projects such as the video integrated management platform, when it comes to the two-way communication between the server and the client , It needs to be implemented through another protocol, the websocket protocol. Under the websocket protocol, the server can actively push information to the client, and the client can also actively send information to the server, which is a true two-way equal dialogue.

99.png

When we use the EasyCVR security monitoring intelligent analysis system to connect to a third-party platform, when a connection is established with a third-party platform through a websocket long connection, the network is suddenly disconnected at this time, or the third-party service crashes and restarts, which means that the connection is suddenly interrupted, which will cause the establishment The websocket link cannot be reconnected again, and the program message has been blocked.

When the program starts, the link status printed is as follows:

38.png

The third-party server is in the request state:

39.png

When the server restarts or the link is disconnected, the client crashes and the program is blocked:

40.png

At this time, the client code to establish the connection code is as follows:

//ping
upaliServer.ping()
writeFunc := func(params []byte) {
       upaliServer.WriteMessage(websocket.BinaryMessage, params)
}
cseqFunc := func() uint32 {
       return RequestCstq.getValue()
}
//初始化api
messapi.Init(writeFunc, cseqFunc)

This code is actually not perfect enough, so here we first define the link url, then start the ping monitoring method after establishing the connection, and then try to establish a connection with the server in a loop after the link is disconnected, if it fails, wait for a period of time and then again Try and exit the process trying to establish a connection after success.

log.Printf("connecting to %s", s.url.String())
wsktDlr := websocket.Dialer{TLSClientConfig: &tls.Config{RootCAs: nil, InsecureSkipVerify: true}}
c, hrsp, err := wsktDlr.Dial(s.url.String(), nil)
if err != nil {
       log.Printf("dial err %s", err.Error())
       time.Sleep(time.Second * 10)
       continue
}
if hrsp == nil && hrsp.StatusCode != 200 {
       log.Printf("dial hrsp %v", hrsp)
       time.Sleep(time.Second)
       continue
}
s.client = c
s.connected = true

Test again when the server is disconnected, the client can see that it is trying to reconnect.

41.png

When the server restarts, the client can quickly connect to it, and the problem has been resolved at this time.

42.png

Guess you like

Origin blog.csdn.net/TsingSee/article/details/114998562