为什么在尝试通过wss://
连接到websocket时出现此错误,但似乎与ws://
一起使用?
WebSocket connection failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR
package main
import (
"fmt"
"log"
"net/http"
//"encoding/json"
"github.com/gorilla/websocket"
)
var clients map[*websocket.Conn]*Client
var upgrader = websocket.Upgrader{
CheckOrigin : func(r *http.Request) bool{
return true
},
}
type Client struct{
session_id string
block_id uint
module string
}
func main(){
var port uint = 8000
http.HandleFunc("/", handleConnections)
log.Printf("Websocket server started on: %d", port)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func handleConnections(w http.ResponseWriter, r *http.Request){
// Upgrade initial GET request to a websocket
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print(err)
return
}
defer ws.Close()
log.Print("Connection established to IP: ", r.RemoteAddr)
...
}
答案 0 :(得分:2)
您的服务器仅侦听HTTP连接,因此您在建立连接时应使用ws
。 wss
是“WebSocket Secure”,它实际上只是指通过HTTPS(TLS)提供的WebSocket连接
来自RFC 6455
A |wss| URI identifies a WebSocket server and resource name and
indicates that traffic over that connection is to be protected via
TLS (including standard benefits of TLS such as data confidentiality
and integrity and endpoint authentication).