Skip to content

Commit f1f1c85

Browse files
authored
Add unmount handler (#63)
1 parent 07db72d commit f1f1c85

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

engine.go

+12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ type Engine interface {
2626
// is called on initial GET request and later when the websocket connects.
2727
// Data to render the handler should be fetched here and returned.
2828
Mount() MountHandler
29+
// UnmountHandler the func that is called by a handler to report that a connection
30+
// is closed. This is called on websocket close. Can be used to track number of
31+
// connected users.
32+
Unmount() UnmountHandler
2933
// Params called to handle any incoming paramters after mount.
3034
Params() []EventHandler
3135
// Render is called to generate the HTML of a Socket. It is defined
@@ -108,6 +112,10 @@ func (e *BaseEngine) Mount() MountHandler {
108112
return e.handler.getMount()
109113
}
110114

115+
func (e *BaseEngine) Unmount() UnmountHandler {
116+
return e.handler.getUnmount()
117+
}
118+
111119
func (e *BaseEngine) Params() []EventHandler {
112120
return e.handler.getParams()
113121
}
@@ -180,6 +188,10 @@ func (e *BaseEngine) DeleteSocket(sock Socket) {
180188
e.socketsMu.Lock()
181189
defer e.socketsMu.Unlock()
182190
delete(e.socketMap, sock.ID())
191+
err := e.Unmount()(sock)
192+
if err != nil {
193+
log.Println("socket unmount error", err)
194+
}
183195
}
184196

185197
// CallEvent route an event to the correct handler.

handler.go

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ type HandlerConfig func(h Handler) error
1818
// in the socket.
1919
type MountHandler func(ctx context.Context, c Socket) (interface{}, error)
2020

21+
// UnmountHandler the func that is called by a handler to report that a connection
22+
// is closed. This is called on websocket close. Can be used to track number of
23+
// connected users.
24+
type UnmountHandler func(c Socket) error
25+
2126
// RenderHandler the func that is called to render the current state of the
2227
// data for the socket.
2328
type RenderHandler func(ctx context.Context, rc *RenderContext) (io.Reader, error)
@@ -39,6 +44,8 @@ type Handler interface {
3944
// HandleMount handles initial setup on first request, and then later when
4045
// the socket first connets.
4146
HandleMount(handler MountHandler)
47+
// HandleUnmount used to track webcocket disconnections.
48+
HandleUnmount(handler UnmountHandler)
4249
// HandleRender used to set the render method for the handler.
4350
HandleRender(handler RenderHandler)
4451
// HandleError for when an error occurs.
@@ -54,6 +61,7 @@ type Handler interface {
5461
HandleParams(handler EventHandler)
5562

5663
getMount() MountHandler
64+
getUnmount() UnmountHandler
5765
getRender() RenderHandler
5866
getError() ErrorHandler
5967
getEvent(t string) (EventHandler, error)
@@ -67,6 +75,8 @@ type BaseHandler struct {
6775
// is called on initial GET request and later when the websocket connects.
6876
// Data to render the handler should be fetched here and returned.
6977
mountHandler MountHandler
78+
// unmountHandler used to track webcocket disconnections.
79+
unmountHandler UnmountHandler
7080
// Render is called to generate the HTML of a Socket. It is defined
7181
// by default and will render any template provided.
7282
renderHandler RenderHandler
@@ -90,6 +100,9 @@ func NewHandler(configs ...HandlerConfig) *BaseHandler {
90100
mountHandler: func(ctx context.Context, s Socket) (interface{}, error) {
91101
return nil, nil
92102
},
103+
unmountHandler: func(s Socket) error {
104+
return nil
105+
},
93106
renderHandler: func(ctx context.Context, rc *RenderContext) (io.Reader, error) {
94107
return nil, ErrNoRenderer
95108
},
@@ -112,6 +125,9 @@ func NewHandler(configs ...HandlerConfig) *BaseHandler {
112125
func (h *BaseHandler) HandleMount(f MountHandler) {
113126
h.mountHandler = f
114127
}
128+
func (h *BaseHandler) HandleUnmount(f UnmountHandler) {
129+
h.unmountHandler = f
130+
}
115131
func (h *BaseHandler) HandleRender(f RenderHandler) {
116132
h.renderHandler = f
117133
}
@@ -140,6 +156,9 @@ func (h *BaseHandler) HandleParams(handler EventHandler) {
140156
func (h *BaseHandler) getMount() MountHandler {
141157
return h.mountHandler
142158
}
159+
func (h *BaseHandler) getUnmount() UnmountHandler {
160+
return h.unmountHandler
161+
}
143162
func (h *BaseHandler) getRender() RenderHandler {
144163
return h.renderHandler
145164
}

0 commit comments

Comments
 (0)