2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
2024-04-27 16:16:30 +03:00
|
|
|
import "time"
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
// SessionStartListener is the listener interface of a session start event
|
|
|
|
type SessionStartListener interface {
|
2024-09-12 14:05:11 +03:00
|
|
|
// OnStart is a function that is called by the library after the creation of the root view of the application
|
2021-09-07 17:36:50 +03:00
|
|
|
OnStart(session Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionFinishListener is the listener interface of a session start event
|
|
|
|
type SessionFinishListener interface {
|
2024-09-12 14:05:11 +03:00
|
|
|
// OnFinish is a function that is called by the library when the user closes the application page in the browser
|
2021-09-07 17:36:50 +03:00
|
|
|
OnFinish(session Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionResumeListener is the listener interface of a session resume event
|
|
|
|
type SessionResumeListener interface {
|
2024-09-12 14:05:11 +03:00
|
|
|
// OnResume is a function that is called by the library when the application page in the client's browser becomes
|
|
|
|
// active and is also called immediately after OnStart
|
2021-09-07 17:36:50 +03:00
|
|
|
OnResume(session Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionPauseListener is the listener interface of a session pause event
|
|
|
|
type SessionPauseListener interface {
|
2024-09-12 14:05:11 +03:00
|
|
|
// OnPause is a function that is called by the library when the application page in the client's browser becomes
|
|
|
|
// inactive and is also called when the user switches to a different browser tab/window, minimizes the browser,
|
|
|
|
// or switches to another application
|
2021-09-07 17:36:50 +03:00
|
|
|
OnPause(session Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionPauseListener is the listener interface of a session disconnect event
|
|
|
|
type SessionDisconnectListener interface {
|
2024-09-12 14:05:11 +03:00
|
|
|
// OnDisconnect is a function that is called by the library if the server loses connection with the client and
|
|
|
|
// this happens when the connection is broken
|
2021-09-07 17:36:50 +03:00
|
|
|
OnDisconnect(session Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionPauseListener is the listener interface of a session reconnect event
|
|
|
|
type SessionReconnectListener interface {
|
2024-09-12 14:05:11 +03:00
|
|
|
// OnReconnect is a function that is called by the library after the server reconnects with the client
|
|
|
|
// and this happens when the connection is restored
|
2021-09-07 17:36:50 +03:00
|
|
|
OnReconnect(session Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *sessionData) onStart() {
|
|
|
|
if session.content != nil {
|
|
|
|
if listener, ok := session.content.(SessionStartListener); ok {
|
|
|
|
listener.OnStart(session)
|
|
|
|
}
|
|
|
|
session.onResume()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *sessionData) onFinish() {
|
|
|
|
if session.content != nil {
|
|
|
|
session.onPause()
|
|
|
|
if listener, ok := session.content.(SessionFinishListener); ok {
|
|
|
|
listener.OnFinish(session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *sessionData) onPause() {
|
|
|
|
if session.content != nil {
|
2024-04-27 16:16:30 +03:00
|
|
|
session.pauseTime = time.Now().Unix()
|
2021-09-07 17:36:50 +03:00
|
|
|
if listener, ok := session.content.(SessionPauseListener); ok {
|
|
|
|
listener.OnPause(session)
|
|
|
|
}
|
2024-04-27 16:16:30 +03:00
|
|
|
if timeout := session.app.Params().SocketAutoClose; timeout > 0 {
|
|
|
|
go session.autoClose(session.pauseTime, timeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *sessionData) autoClose(start int64, timeout int) {
|
|
|
|
time.Sleep(time.Second * time.Duration(timeout))
|
|
|
|
if session.pauseTime == start {
|
|
|
|
session.bridge.callFunc("closeSocket")
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *sessionData) onResume() {
|
2024-04-27 16:16:30 +03:00
|
|
|
session.pauseTime = 0
|
2021-09-07 17:36:50 +03:00
|
|
|
if session.content != nil {
|
|
|
|
if listener, ok := session.content.(SessionResumeListener); ok {
|
|
|
|
listener.OnResume(session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *sessionData) onDisconnect() {
|
|
|
|
if session.content != nil {
|
|
|
|
if listener, ok := session.content.(SessionDisconnectListener); ok {
|
|
|
|
listener.OnDisconnect(session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *sessionData) onReconnect() {
|
|
|
|
if session.content != nil {
|
|
|
|
if listener, ok := session.content.(SessionReconnectListener); ok {
|
|
|
|
listener.OnReconnect(session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|