猿问

websocket:客户端未使用 websocket 协议:“连接”标头中未找到“升级”令牌

我正在尝试与用 Go 和 JavaScript 前端编写的服务器建立 websocket 连接。我在一个目录中有以下文件:


main.go 索引.html


**这是我的 Go 代码main.go:**


package main



import (

    "fmt"

    "log"

    "net/http"


    "github.com/gorilla/websocket"

)


var upgrader = websocket.Upgrader{

    ReadBufferSize:  1024,

    WriteBufferSize: 1024,

}


func homePage(w http.ResponseWriter, r *http.Request) {

    http.ServeFile(w, r, "./index.html")

    conn, err := upgrader.Upgrade(w, r, nil)

    if err != nil {

        log.Println("Error in handler:", err)

        return

    }

    log.Println("Client connected.")


    for {

        messageType, p, err := conn.ReadMessage()

        if err != nil {

            log.Println("Fehler in ReadMessage: ", err)

            return

        }


        log.Println(string(p))


        //echo message to client

        if err := conn.WriteMessage(messageType, p); err != nil {

            log.Println(err)

            return

        }

    }

}


func setupRoutes() {

    http.HandleFunc("/ws", homePage)

}


func main() {

    fmt.Println("Server gestartet")

    setupRoutes()

    log.Fatal(http.ListenAndServe(":9100", nil))


}


这是 index.html 中的 HTML 和 JavaScript:


<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Some unimportant html </title>

</head>

<body>

    

    <script>



        let socket = new WebSocket("ws://localhost:9100/ws");

        console.log("Websocket started.");


        socket.onOpen = () => {

            console.log("Client started.");

        }


        socket.onclose = (event) => {

            console.log("Socket closed: ", event);

        }


        socket.onError = (error) => {

            console.log("Socket Error: ", error);

        }


        socket.onMessage = (msg) => {

            console.log(msg);

        }

    </script>

</body>

</html>


GCT1015
浏览 104回答 1
1回答

胡子哥哥

我得以修复它。问题是我需要一个端点用于索引文件,另一个端点用于 websocket。所以我将相关部分更改为:func websocketHandler(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; conn, err := upgrader.Upgrade(w, r, nil) //conn is a websocket connection (aus http wird websocket protokoll)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Println("Error in handler:", err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; log.Println("Client connected.")&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; messageType, p, err := conn.ReadMessage()&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println("Fehler in ReadMessage: ", err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; log.Println(string(p))&nbsp; &nbsp; &nbsp; &nbsp; //echo message to client&nbsp; &nbsp; &nbsp; &nbsp; if err := conn.WriteMessage(messageType, p); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func homePage(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; http.ServeFile(w, r, "./index.html")}func setupRoutes() {&nbsp; &nbsp; http.HandleFunc("/", homePage)&nbsp; &nbsp; http.HandleFunc("/ws", websocketHandler)}
随时随地看视频慕课网APP

相关分类

Go
我要回答