diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index dfb256a..5ebd503 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -7,10 +7,10 @@ jobs: runs-on: ubuntu-latest steps: - - name: Set up Go 1.12 + - name: Set up Go 1.14 uses: actions/setup-go@v1 with: - version: 1.12 + version: 1.14 id: go - name: Check out code into the Go module directory diff --git a/_examples/basic/main.go b/_examples/basic/main.go index bdfda2d..decd2e6 100644 --- a/_examples/basic/main.go +++ b/_examples/basic/main.go @@ -155,6 +155,14 @@ func startServer() { log.Printf("[%s] connected to the server.", c) + // If you want to close the connection immediately + // from server's OnConnect event then you should + // set the `FireDisconnectAlways` option to true. + // ws.FireDisconnectAlways = true: + // + // return fmt.Errorf("custome rror") + // c.Close() + // if returns non-nil error then it refuses the client to connect to the server. return nil } diff --git a/_examples/cronjob/main.go b/_examples/cronjob/main.go index 474608c..c7fbfe5 100644 --- a/_examples/cronjob/main.go +++ b/_examples/cronjob/main.go @@ -38,7 +38,7 @@ const ( var ( // all these fields should located to your app's structure and designing - // however, for the shake of the example we will declare them as package-level variables here. + // however, for the sake of the example we will declare them as package-level variables here. database *dbMock websocketServer *neffos.Server ) @@ -171,7 +171,7 @@ func (db *dbMock) removeNotification(nfID string) { } // Accept user ids and get all of their notifications as one notification slice. -// Why? For the shake of the example, see the comments on the `pushNotifications`. +// Why? For the sake of the example, see the comments on the `pushNotifications`. func (db *dbMock) getNotificationList(userIDs []string) (list []notification) { db.mu.RLock() for _, userID := range userIDs { @@ -219,7 +219,7 @@ func upsertNotificationHandler(db *dbMock) http.HandlerFunc { } // Declare them on a custom struct of course, -// they are exposed like that for the shake of the example. +// they are exposed like that for the sake of the example. var ( // write access on websocketServer.OnConnect and OnDisconnect callbacks. // read access on pushNotifications() function. diff --git a/_examples/struct-handler/main.go b/_examples/struct-handler/main.go index b841643..7c655e6 100644 --- a/_examples/struct-handler/main.go +++ b/_examples/struct-handler/main.go @@ -57,7 +57,7 @@ func (s *clientConn) ChatResponse(msg neffos.Message) error { // $ go run main.go server // $ go run main.go client // # expected output: -// # Echo back from server: Hello from client!Static Response Suffix for shake of the example. +// # Echo back from server: Hello from client!Static Response Suffix for sake of the example. func main() { neffos.EnableDebug(nil) @@ -80,7 +80,7 @@ func main() { func startServer() { controller := new(serverConn) - controller.SuffixResponse = "Static Response Suffix for shake of the example" + controller.SuffixResponse = "Static Response Suffix for sake of the example" // This will convert a structure to neffos.Namespaces based on the struct's methods. // The methods can be func(msg neffos.Message) error if the structure contains a *neffos.NSConn field, diff --git a/server.go b/server.go index 1594c08..509c5f6 100644 --- a/server.go +++ b/server.go @@ -59,6 +59,13 @@ type Server struct { // Therefore, if set to true, // each broadcast call will publish its own message(s) by order. SyncBroadcaster bool + // FireDisconnectAlways will allow firing the `OnDisconnect` server's + // event even if the connection wasimmediately closed from the `OnConnect` server's event + // through `Close()` or non-nil error. + // See https://github.com/kataras/neffos/issues/41 + // + // Defaults to false. + FireDisconnectAlways bool mu sync.RWMutex namespaces Namespaces @@ -173,7 +180,7 @@ func (s *Server) start() { // println("disconnect...") if s.OnDisconnect != nil { // don't fire disconnect if was immediately closed on the `OnConnect` server event. - if !c.readiness.isReady() || (c.readiness.err != nil) { + if !s.FireDisconnectAlways && (!c.readiness.isReady() || (c.readiness.err != nil)) { continue } s.OnDisconnect(c)