File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,49 @@ func main() {
76
76
}
77
77
```
78
78
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
+
79
122
## TODO
80
123
81
124
The following is in no particular order. Please open an Issue for priority or open a PR to contribute to this list.
You can’t perform that action at this time.
0 commit comments