Go建立WebSocket與HTTP server。
範例環境:
- Go 1.19
範例
參考「Golang 建立網頁伺服器 Web Server」及「Golang WebSocket hello world」,WebSocket和HTTP server共用同個port。
main.go
package main
import (
"fmt"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true // allow CORS request's Origin header
},
}
func main() {
http.HandleFunc("/echo", echoHandler) // websocket endpoint
http.HandleFunc("/hello", helloHandler) // http endpoint
http.ListenAndServe(":8080", nil)
}
func echoHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
panic(err)
}
defer conn.Close()
for {
mt, message, err := conn.ReadMessage()
if err != nil {
panic(err)
}
fmt.Printf("receive: %s", message)
err = conn.WriteMessage(mt, message)
if err != nil {
panic(err)
}
}
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
content := fmt.Sprintf("hello, %s", name)
fmt.Fprint(w, content)
}
沒有留言:
張貼留言