Your Client <https://play.golang.org/p/CmXjfAJSCY7> code does not compile. As a courtesy to those trying to help, it is always good to test your code before posting it. A working example, without your "busy" messages would be something like: https://play.golang.org/p/cvgwd8GDobc . The Read() method documentation <https://godoc.org/golang.org/x/net/websocket#Conn.Read> says "Read implements the io.Reader interface." So I suggest you *carefully *read the io.Reader documentation <https://golang.org/pkg/io/#Reader>, because it requires very specific handling to be done correctly.
Also, I did note that golang.org/x/net/websocket documentation <https://godoc.org/golang.org/x/net/websocket> states that: "This package currently lacks some features found in alternative and more actively maintained WebSocket packages." So you may want to consider using one of those. I have not actually used golang.org/x/net <https://godoc.org/golang.org/x/net>/websocket myself, but there is a SetReadDeadline <https://godoc.org/golang.org/x/net/websocket#Conn.SetReadDeadline>() function. Because I have not used the package before, there may be some hidden pitfalls that I am missing, but this code seems to do what you want, using SetReadDeadline <https://godoc.org/golang.org/x/net/websocket#Conn.SetReadDeadline>(): https://play.golang.org/p/ISUR7c_Mtqb Good luck On Wednesday, March 25, 2020 at 8:56:57 PM UTC-4, 洪嘉鴻 wrote: > > I'm sorry. > The followings are the complete codes of server and client: > Server <https://play.golang.org/p/9vu0GW5a5Y8> > Client <https://play.golang.org/p/CmXjfAJSCY7> > > Thank you very much! > Max > > Jake Montgomery於 2020年3月25日星期三 UTC+8下午9時28分37秒寫道: >> >> You do not provide enough information to give a really good answer. There >> are a number of ways to achieve this, but they depend on the details. A >> more complete example would help. >> >> What is the type of `ws`? If it is something on which you can set a read >> timeout, then one way would be to set a timeout, and loop. >> >> On Wednesday, March 25, 2020 at 4:10:19 AM UTC-4, 洪嘉鴻 wrote: >>> >>> Hello everyone: >>> The version of the golang which I am using is 1.12.9 with Win10. >>> I want to print something while the client is waiting for server. >>> The following are the partial code of server and client. >>> >>> Server: >>> time.Sleep(time.Second * 100) >>> ws.Write([]byte("Hello!")) >>> >>> Client: >>> while(!ws.Read(msg)){ >>> fmt.Println("Please wait because server is busy.") >>> } >>> >>> Can anyone suggest how to fix the code? >>> >>> Any help is appreciated. >>> Thank you very much! >>> Max >>> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c1e09631-eea3-40e2-bb5a-246906e17508%40googlegroups.com.