WebSocket连接失败:连接建立错误:net::ERR_SSL_PROTOCOL_ERROR

huangapple go评论114阅读模式
英文:

WebSocket connection failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR

问题

尝试使用 wss:// 连接到 WebSocket 时,为什么会出现以下错误,但使用 ws:// 却可以正常工作?

#error

WebSocket connection failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR

#code
package main

import (
    "fmt"
    "log"
    "net/http"
    "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)

    ...
}
英文:

Why do I get this error when trying to connect to a websocket via wss:// but seems to work fine with ws://?

#error

WebSocket connection failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR

#code
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

得分: 2

您的服务器只监听HTTP连接,因此在建立连接时应使用wswss是“WebSocket Secure”的缩写,实际上指的是通过HTTPS(TLS)提供的WebSocket连接。

根据RFC 6455

wss URI标识了一个WebSocket服务器和资源名称,并指示通过该连接的流量将通过TLS进行保护(包括TLS的标准优点,如数据机密性、完整性和端点身份验证)。

英文:

Your server is only listening for HTTP connections so you should use ws when making a connection. wss is "WebSocket Secure" which really just means a WebSocket connection that is served over HTTPS (TLS)

From the 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).

huangapple
  • 本文由 发表于 2017年3月9日 05:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/42682436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定