Handling a disconnection event

This leads us to the last function, HandleDisconnection, which is called either on an abnormal WebSocket connection close event or when the internet connection has been disconnected, that is, when the wenv.Window object fires an offline event:

func HandleDisconnection(env *common.Env) {

chatContainer := env.Document.GetElementByID("chatboxContainer").(*dom.HTMLDivElement)
chatContainer.SetClass("")

chatboxHeaderBar := env.Document.GetElementByID("chatboxHeaderBar").(*dom.HTMLDivElement)
chatboxHeaderBar.SetClass("chatboxHeader disconnected")

chatboxTitleDiv := env.Document.GetElementByID("chatboxTitle").(*dom.HTMLDivElement)
if chatboxTitleDiv != nil {
titleSpan := chatboxTitleDiv.ChildNodes()[0].(*dom.HTMLSpanElement)
if titleSpan != nil {
var countdown uint64 = 6
tickerForCountdown := time.NewTicker(1 * time.Second)
timerToCloseChat := time.NewTimer(6 * time.Second)
go func() {
for _ = range tickerForCountdown.C {
atomic.AddUint64(&countdown, ^uint64(0))
safeCountdownValue := atomic.LoadUint64(&countdown)
titleSpan.SetInnerHTML("Disconnected! - Closing LiveChat in " + strconv.FormatUint(safeCountdownValue, 10) + " seconds.")
}
}()
go func() {
<-timerToCloseChat.C
tickerForCountdown.Stop()
CloseChat(env)
}()
}
}
}

We first set the CSS classname value of the chatContainer to empty string, using the SetClass method, to disable the chatContainer  element's pulsate effect to indicate that the connection has been broken.

We we then change the background color of chatboxHeaderBar to red by setting the chatboxHeaderBar element's CSS classname value to "chatboxHeader disconnected" using the SetClass method. 

The remaining code will present the user with a message indicating that the connection has been disconnected, and the live chat feature will automatically initiate a countdown. The chatboxHeaderBar will display the countdown, 5-4-3-2-1, by the second, as the live chat feature shuts itself down. We use two goroutines, one for the countdown ticker and the other for the countdown timer. When the countdown timer has expired, it signifies that the countdown is over, and we call the CloseChat function passing in the env object to close the live chat feature.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.199.181