websocket_client.go 514 B

1234567891011121314151617181920212223242526272829
  1. // websocket_client.go
  2. package main
  3. import (
  4. "fmt"
  5. "time"
  6. "code.google.com/p/go.net/websocket"
  7. )
  8. func main() {
  9. ws, err := websocket.Dial("ws://localhost:12345/websocket", "",
  10. "http://localhost/")
  11. if err != nil {
  12. panic("Dial: " + err.Error())
  13. }
  14. go readFromServer(ws)
  15. time.Sleep(5e9)
  16. ws.Close()
  17. }
  18. func readFromServer(ws *websocket.Conn) {
  19. buf := make([]byte, 1000)
  20. for {
  21. if _, err := ws.Read(buf); err != nil {
  22. fmt.Printf("%s\n", err.Error())
  23. break
  24. }
  25. }
  26. }