这次更新带来了客户端支持, 与JS类似的WebSocket API
### Example
```go
package main
import (
"fmt"
"github.com/lxzan/gws"
"log"
)
func main() {
option := &gws.ClientOption{Addr: "ws://127.0.0.1:3000/connect"}
socket, _, err := gws.NewClient(new(WebSocket), option)
if err != nil {
log.Printf(err.Error())
return
}
socket.Listen()
}
type WebSocket struct{}
func (c *WebSocket) OnError(socket *gws.Conn, err error) {
fmt.Printf("onerror: err=%s\n", err.Error())
}
func (c *WebSocket) OnClose(socket *gws.Conn, code uint16, reason []byte) {
fmt.Printf("onclose: code=%d, payload=%s\n", code, string(reason))
}
func (c *WebSocket) OnPong(socket *gws.Conn, payload []byte) {
}
func (c *WebSocket) OnOpen(socket *gws.Conn) {
_ = socket.WriteString("hello, there is client")
}
func (c *WebSocket) OnPing(socket *gws.Conn, payload []byte) {
socket.WritePong(payload)
}
func (c *WebSocket) OnMessage(socket *gws.Conn, message *gws.Message) {
fmt.Printf("recv: %s\n", message.Data.String())
}
```
有疑问加站长微信联系(非本文作者)