Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

客户端没有readMessage方法 #117

Open
mebest100 opened this issue Oct 15, 2024 · 5 comments
Open

客户端没有readMessage方法 #117

mebest100 opened this issue Oct 15, 2024 · 5 comments
Labels
help wanted Extra attention is needed stale

Comments

@mebest100
Copy link

mebest100 commented Oct 15, 2024

就像下面的代码, 创建出来的客户端连接只能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
Copy link
Owner

lxzan commented Oct 15, 2024

有 OnMessage

@lxzan lxzan added the help wanted Extra attention is needed label Oct 15, 2024
@mebest100
Copy link
Author

mebest100 commented Oct 15, 2024

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

@lxzan
Copy link
Owner

lxzan commented Oct 15, 2024

@mebest100
Copy link
Author

mebest100 commented Oct 15, 2024

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


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())
}

Copy link

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

@github-actions github-actions bot added the stale label Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed stale
Projects
None yet
Development

No branches or pull requests

2 participants