Skip to content

docs: Updated to include information on custom event callbacks #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -76,6 +76,49 @@ func main() {
}
```

### A more complicated example: emitting and listening to a custom event
```go
import (
"log"
"net/http"
"time"

sio "github.com/njones/socketio"
eio "github.com/njones/socketio/engineio"
eiot "github.com/njones/socketio/engineio/transport"
ser "github.com/njones/socketio/serialize"
)

// Define a custom wrapper
type CustomWrap func(string, string) error

// Define your callback
func (cc CustomWrap) Callback(data ...interface{}) error {
a, aOK := data[0].(string)
b, bOK := data[1].(string)

if !aOK || !bOK {
return fmt.Errorf("bad parameters")
}

return cc(a, b)
}

func main() {
port := ":3000"
server := socketio.NewServer()
server.OnConnect(func(socket *sio.SocketV4) error {
// Implement your callback for a custom event
socket.On("myEvent", CustomWrap(func(a string, b string) error{
socket.emit("hello", a, b)
return nil
})
}
log.Printf("serving port %s...\n", port)
log.Fatal(http.ListenAndServe(port, server))
}
```

## TODO

The following is in no particular order. Please open an Issue for priority or open a PR to contribute to this list.