Skip to content

Commit

Permalink
add 'FireDisconnectAlways' option to enable calling the local OnDisco…
Browse files Browse the repository at this point in the history
…nnect event if the server closed the connection from its OnConnect event: #41
  • Loading branch information
kataras committed May 18, 2020
1 parent f143186 commit 15e3a17
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions _examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions _examples/cronjob/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions _examples/struct-handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 15e3a17

Please sign in to comment.