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_server.go
Normal file
29
eBook/examples/chapter_15/websocket_server.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// websocket_server.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"code.google.com/p/go.net/websocket"
|
||||
)
|
||||
|
||||
func server(ws *websocket.Conn) {
|
||||
fmt.Printf("new connection\n")
|
||||
buf := make([]byte, 100)
|
||||
for {
|
||||
if _, err := ws.Read(buf); err != nil {
|
||||
fmt.Printf("%s", err.Error())
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Printf(" => closing connection\n")
|
||||
ws.Close()
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.Handle("/websocket", websocket.Handler(server))
|
||||
err := http.ListenAndServe(":12345", nil)
|
||||
if err != nil {
|
||||
panic("ListenAndServe: " + err.Error())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user