dongmale0656 2017-03-08 21:41 采纳率: 100%
浏览 4544
已采纳

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

当我试图通过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)

    ...
}
  • 写回答

1条回答 默认 最新

  • dragon87836215 2017-03-08 21:49
    关注

    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).
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置