mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 04:48:29 +08:00
04.4.md
This commit is contained in:
29
eBook/examples/chapter_15/websocket_client.go
Normal file
29
eBook/examples/chapter_15/websocket_client.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// websocket_client.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"code.google.com/p/go.net/websocket"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ws, err := websocket.Dial("ws://localhost:12345/websocket", "",
|
||||
"http://localhost/")
|
||||
if err != nil {
|
||||
panic("Dial: " + err.Error())
|
||||
}
|
||||
go readFromServer(ws)
|
||||
time.Sleep(5e9)
|
||||
ws.Close()
|
||||
}
|
||||
|
||||
func readFromServer(ws *websocket.Conn) {
|
||||
buf := make([]byte, 1000)
|
||||
for {
|
||||
if _, err := ws.Read(buf); err != nil {
|
||||
fmt.Printf("%s\n", err.Error())
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user