go-websocket-client
golang simply implement of websocket client base on gorilla/websocket
install
go get -u github.com/link-yundi/go-websocket-client
example
configuration
import github.com/link-yundi/go-websocket-client
func main() {
...
config := Config{
Url: "ws host",
ProxyUrl: "proxy host",
PingPeriod: 15 * time.Second,
IsAutoReconnect: true,
}
client := NewClient(config)
...
}
start client
...
client.Start()
...
subscribe
...
client.Subscribe([]byte(`some event string`))
...
auto reconnect
...
config := Config{
...
IsAutoReconnect: true,
...
}
...
close client
...
client.Terminate()
...
customize message-handler
func onMessage(msg []byte) {
// do somthing you want
fmt.Println("OnMessage: ", string(msg))
}
func onConnect() {
// do somthing you want
fmt.Println("OnConnect")
}
func onClose(code int, text string) error {
// do somthing you want
fmt.Println("OnClose", code, text)
return nil
}
func main() {
config := Config{
...
OnMessage: onMessage,
OnClose: onClose,
OnConnect: onConnect,
PingPeriod: 15 * time.Second,
IsAutoReconnect: true,
...
}
client := NewClient(config)
go client.Start()
// or client.Start()
}