1 回答
TA贡献1825条经验 获得超6个赞
我得以修复它。问题是我需要一个端点用于索引文件,另一个端点用于 websocket。所以我将相关部分更改为:
func websocketHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil) //conn is a websocket connection (aus http wird websocket protokoll)
if err != nil {
log.Println("Error in handler:", err)
return
}
log.Println("Client connected.")
for {
messageType, p, err := conn.ReadMessage()
if err != nil {
log.Println("Fehler in ReadMessage: ", err)
return
}
log.Println(string(p))
//echo message to client
if err := conn.WriteMessage(messageType, p); err != nil {
log.Println(err)
return
}
}
}
func homePage(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./index.html")
}
func setupRoutes() {
http.HandleFunc("/", homePage)
http.HandleFunc("/ws", websocketHandler)
}
- 1 回答
- 0 关注
- 103 浏览
添加回答
举报