我在使用 WebSockets 协议时遇到 Go 问题。如果我连接到我的 API,一切都会正常。如果我添加“协议”,例如“Hey”,它会开始循环多次,并以错误结束*github.com/gorilla/websocket.CloseError:“Code 1006,Text Unexpected EOF”。
Sec-Websocket-Protocol我绝对不明白为什么当我在连接中发送时它会这样。
有我的代码:
main.go
package main
import (
"fmt"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
stacktracer "gitlab.com/eyes-eyes/internals-stacktracer"
"gitlab.com/eyesbank/go-web-sockets-server/handlers"
"net/http"
)
const webSocketsAddr = "0.0.0.0:8082"
// main is the starting point of the current micro service.
func main() {
// Setting the service name
stacktracer.SetServiceName("Hello 'X' (Web Sockets)")
// Initializing the HTTP errors handling
runtime.HTTPError = stacktracer.DefaultHTTPError
if err := RunWebSocketsServer(); err != nil {
glog.Fatal(err)
}
}
//
// WebSocket
//
func RunWebSocketsServer() error {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handlers.HandleUserSocket(w, r)
})
fmt.Println(webSocketsAddr)
return http.ListenAndServe(webSocketsAddr, nil)
}
func RunWebSocketsTLSServer() error {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handlers.HandleUserSocket(w, r)
})
fmt.Println(webSocketsAddr)
return http.ListenAndServeTLS(webSocketsAddr, "server.crt", "server.key", nil)
}
阿波罗的战车
相关分类