@@ -28,9 +28,11 @@ type Controller interface {
28
28
}
29
29
30
30
type opt struct {
31
- channelFunc func (r * http.Request , viewID string ) * string
32
- pathParamsFunc func (r * http.Request ) PathParams
33
- websocketUpgrader websocket.Upgrader
31
+ onSocketConnect func (userOrSessionID string ) error
32
+ onSocketDisconnect func (userOrSessionID string )
33
+ channelFunc func (r * http.Request , viewID string ) * string
34
+ pathParamsFunc func (r * http.Request ) PathParams
35
+ websocketUpgrader websocket.Upgrader
34
36
35
37
disableTemplateCache bool
36
38
disableWebsocket bool
@@ -146,6 +148,27 @@ func WithDropDuplicateInterval(interval time.Duration) ControllerOption {
146
148
}
147
149
}
148
150
151
+ // WithOnSocketConnect takes a function that is called when a new websocket connection is established.
152
+ // The function should return an error if the connection should be rejected.
153
+ // The user or fir's browser session id is passed to the function.
154
+ // user must be set in request.Context with the key UserKey by a developer supplied authentication mechanism.
155
+ // It can be used to track user connections and disconnections.
156
+ // It can be be used to reject connections based on user or session id.
157
+ // It can be used to refresh the page data when a user re-connects.
158
+ func WithOnSocketConnect (f func (userOrSessionID string ) error ) ControllerOption {
159
+ return func (o * opt ) {
160
+ o .onSocketConnect = f
161
+ }
162
+ }
163
+
164
+ // WithOnSocketDisconnect takes a function that is called when a websocket connection is disconnected.
165
+ func WithOnSocketDisconnect (f func (userOrSessionID string )) ControllerOption {
166
+ return func (o * opt ) {
167
+ o .onSocketDisconnect = f
168
+ }
169
+
170
+ }
171
+
149
172
// DisableTemplateCache is an option to disable template caching. This is useful for development.
150
173
func DisableTemplateCache () ControllerOption {
151
174
return func (o * opt ) {
0 commit comments