Skip to content

Commit c34fad1

Browse files
authored
docs: Updated to include information on custom event callbacks (#71)
1 parent eef8da7 commit c34fad1

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Diff for: README.md

+43
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,49 @@ func main() {
7676
}
7777
```
7878

79+
### A more complicated example: emitting and listening to a custom event
80+
```go
81+
import (
82+
"log"
83+
"net/http"
84+
"time"
85+
86+
sio "github.com/njones/socketio"
87+
eio "github.com/njones/socketio/engineio"
88+
eiot "github.com/njones/socketio/engineio/transport"
89+
ser "github.com/njones/socketio/serialize"
90+
)
91+
92+
// Define a custom wrapper
93+
type CustomWrap func(string, string) error
94+
95+
// Define your callback
96+
func (cc CustomWrap) Callback(data ...interface{}) error {
97+
a, aOK := data[0].(string)
98+
b, bOK := data[1].(string)
99+
100+
if !aOK || !bOK {
101+
return fmt.Errorf("bad parameters")
102+
}
103+
104+
return cc(a, b)
105+
}
106+
107+
func main() {
108+
port := ":3000"
109+
server := socketio.NewServer()
110+
server.OnConnect(func(socket *sio.SocketV4) error {
111+
// Implement your callback for a custom event
112+
socket.On("myEvent", CustomWrap(func(a string, b string) error{
113+
socket.emit("hello", a, b)
114+
return nil
115+
})
116+
}
117+
log.Printf("serving port %s...\n", port)
118+
log.Fatal(http.ListenAndServe(port, server))
119+
}
120+
```
121+
79122
## TODO
80123

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

0 commit comments

Comments
 (0)