2017-03-08 216 views
1

爲什麼我試圖通過wss://連接到websocket時出現此錯誤,但似乎與ws://一起正常工作?WebSocket連接失敗:連接建立時出錯:net :: ERR_SSL_PROTOCOL_ERROR

錯誤

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) 

    ... 
} 

回答

1

服務器僅監聽HTTP連接,所以你應該做一個連接時使用wswss是 「的WebSocket安全」 這真的只是意味着通過HTTPS提供了一個WebSocket連接(TLS)

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). 
相關問題