Skip to content

Commit 15e3a17

Browse files
committed
add 'FireDisconnectAlways' option to enable calling the local OnDisconnect event if the server closed the connection from its OnConnect event: #41
1 parent f143186 commit 15e3a17

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ jobs:
77
runs-on: ubuntu-latest
88
steps:
99

10-
- name: Set up Go 1.12
10+
- name: Set up Go 1.14
1111
uses: actions/setup-go@v1
1212
with:
13-
version: 1.12
13+
version: 1.14
1414
id: go
1515

1616
- name: Check out code into the Go module directory

_examples/basic/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ func startServer() {
155155

156156
log.Printf("[%s] connected to the server.", c)
157157

158+
// If you want to close the connection immediately
159+
// from server's OnConnect event then you should
160+
// set the `FireDisconnectAlways` option to true.
161+
// ws.FireDisconnectAlways = true:
162+
//
163+
// return fmt.Errorf("custome rror")
164+
// c.Close()
165+
158166
// if returns non-nil error then it refuses the client to connect to the server.
159167
return nil
160168
}

_examples/cronjob/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const (
3838

3939
var (
4040
// all these fields should located to your app's structure and designing
41-
// however, for the shake of the example we will declare them as package-level variables here.
41+
// however, for the sake of the example we will declare them as package-level variables here.
4242
database *dbMock
4343
websocketServer *neffos.Server
4444
)
@@ -171,7 +171,7 @@ func (db *dbMock) removeNotification(nfID string) {
171171
}
172172

173173
// Accept user ids and get all of their notifications as one notification slice.
174-
// Why? For the shake of the example, see the comments on the `pushNotifications`.
174+
// Why? For the sake of the example, see the comments on the `pushNotifications`.
175175
func (db *dbMock) getNotificationList(userIDs []string) (list []notification) {
176176
db.mu.RLock()
177177
for _, userID := range userIDs {
@@ -219,7 +219,7 @@ func upsertNotificationHandler(db *dbMock) http.HandlerFunc {
219219
}
220220

221221
// Declare them on a custom struct of course,
222-
// they are exposed like that for the shake of the example.
222+
// they are exposed like that for the sake of the example.
223223
var (
224224
// write access on websocketServer.OnConnect and OnDisconnect callbacks.
225225
// read access on pushNotifications() function.

_examples/struct-handler/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (s *clientConn) ChatResponse(msg neffos.Message) error {
5757
// $ go run main.go server
5858
// $ go run main.go client
5959
// # expected output:
60-
// # Echo back from server: Hello from client!Static Response Suffix for shake of the example.
60+
// # Echo back from server: Hello from client!Static Response Suffix for sake of the example.
6161
func main() {
6262
neffos.EnableDebug(nil)
6363

@@ -80,7 +80,7 @@ func main() {
8080

8181
func startServer() {
8282
controller := new(serverConn)
83-
controller.SuffixResponse = "Static Response Suffix for shake of the example"
83+
controller.SuffixResponse = "Static Response Suffix for sake of the example"
8484

8585
// This will convert a structure to neffos.Namespaces based on the struct's methods.
8686
// The methods can be func(msg neffos.Message) error if the structure contains a *neffos.NSConn field,

server.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ type Server struct {
5959
// Therefore, if set to true,
6060
// each broadcast call will publish its own message(s) by order.
6161
SyncBroadcaster bool
62+
// FireDisconnectAlways will allow firing the `OnDisconnect` server's
63+
// event even if the connection wasimmediately closed from the `OnConnect` server's event
64+
// through `Close()` or non-nil error.
65+
// See https://github.com/kataras/neffos/issues/41
66+
//
67+
// Defaults to false.
68+
FireDisconnectAlways bool
6269

6370
mu sync.RWMutex
6471
namespaces Namespaces
@@ -173,7 +180,7 @@ func (s *Server) start() {
173180
// println("disconnect...")
174181
if s.OnDisconnect != nil {
175182
// don't fire disconnect if was immediately closed on the `OnConnect` server event.
176-
if !c.readiness.isReady() || (c.readiness.err != nil) {
183+
if !s.FireDisconnectAlways && (!c.readiness.isReady() || (c.readiness.err != nil)) {
177184
continue
178185
}
179186
s.OnDisconnect(c)

0 commit comments

Comments
 (0)