lxzan / gws

simple, fast, reliable websocket server & client, supports running over tcp/kcp/unix domain socket. keywords: ws, proxy, chat, go, golang...
https://pkg.go.dev/github.com/lxzan/gws
Apache License 2.0
1.43k stars 94 forks source link

客户端没有readMessage方法 #117

Open mebest100 opened 1 month ago

mebest100 commented 1 month ago

就像下面的代码, 创建出来的客户端连接只能writeMessage写入信息,不能通过readMessage获取到信息,这很不方便。 而gorilla和gobwas都有清楚的实现,我看了你的源码也没有找到相应的内容.

    client, _, err := gws.NewClient(&gws.BuiltinEventHandler{}, &gws.ClientOption{
        Addr:          serverAddr,
        TlsConfig:     tlsConfig,
        RequestHeader: header,
    })

    message := []byte("Hello, WebSocket!")
    err = client.WriteMessage(gws.OpcodeBinary, message)
lxzan commented 1 month ago

有 OnMessage

mebest100 commented 1 month ago

但是OnMessage没法直接用吧,上面的代码中client.后面跟不出来 OnMessage,这个跟gorilla和gobwas很不一样,不太理解。 我现在的需求简单说就是通过读取操作获取一个字符串消息

lxzan commented 1 month ago

看看别人怎么用的 https://github.com/endymx/bot/blob/master/websocket.go

mebest100 commented 1 month ago

我是这样写的,没有问题吧


func main() {
    // 穿越cdn必须使用wss 
    serverAddr := "wss://xxxCDNEdgeIP:443/ws"
    sni := "xxxdomain"

    header := http.Header{} 
    header.Set("Host", sni)

    _, _, err := gws.NewClient(&gws.BuiltinEventHandler{}, &gws.ClientOption{
        Addr: serverAddr,
        TlsConfig: &tls.Config{
            InsecureSkipVerify: true,
            ServerName:         sni,
        },
        RequestHeader: header,
    })

    if err != nil {
        log.Printf("Client create fail, err==>: %v", err)
    }

}

type WebSocket struct {
}

func (c *WebSocket) OnOpen(socket *gws.Conn) {
    _ = socket.WriteString("hello, I'm websocket client")
}

func (c *WebSocket) OnMessage(socket *gws.Conn, message *gws.Message) {
    defer message.Close()
    fmt.Printf("recv: %s\n", message.Data.String())
}
github-actions[bot] commented 1 week ago

This issue is stale because it has been open for 30 days with no activity.