第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Websocket 握手失敗 404(golang 服務(wù)器)

Websocket 握手失敗 404(golang 服務(wù)器)

Go
不負(fù)相思意 2021-12-07 18:34:47
我有一個(gè)簡單的 go web 服務(wù)器,它在端口 localhost:8080 上提供一個(gè)公共文件夾,其中包含一個(gè) html 文件以及一個(gè)帶有 websocket 邏輯的客戶端腳本。在我的main.go 文件中l(wèi)istener, err := net.listen("tcp", "localhost:8080")if err != nil {    log.Fatal(err)}//full code in gist https://gist.github.com/Kielan/98706aaf5dc0be9d6fbe然后在我的客戶端腳本中try {    var sock = new WebSocket("ws://127.0.0.1:8080");    console.log("Websocket - status: " + sock.readyState);    sock.onopen = function(message) {    console.log("CONNECTION opened..." + this.readyState);    //onmessage, onerr, onclose, ect...}我在 chrome 中遇到錯(cuò)誤WebSocket connection to 'ws://127.0.0.1:8080/' failed: Error during WebSocket handshake: Unexpected response code: 200和火狐Firefox can't establish a connection to the server at ws://127.0.0.1:8080/.我發(fā)現(xiàn)這篇文章指的是 node.js 指示將 /websocket 添加到我的客戶端 websocket 字符串,盡管它沒有解決問題并導(dǎo)致 404我認(rèn)為響應(yīng)代碼 200 很好,我是否需要以某種方式將請求轉(zhuǎn)換為 websocket,也許它默認(rèn)為 http?如果是這樣,我該怎么做?
查看完整描述

1 回答

?
BIG陽

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

就像 JimB 指出的那樣,您還沒有處理 http 或 websocket 連接。


您可以使用包進(jìn)行 websocket 處理github.com/gorilla/websocket 這是一個(gè)簡單的設(shè)置:


package main


import (    

    "log"

    "net/http"

    "github.com/gorilla/websocket"

)


// wsHandler implements the Handler Interface

type wsHandler struct{}


func main() {

    router := http.NewServeMux()

    router.Handle("/", http.FileServer(http.Dir("./webroot"))) //handles static html / css etc. under ./webroot

    router.Handle("/ws", wsHandler{}) //handels websocket connections


    //serving

    log.Fatal(http.ListenAndServe("localhost:8080", router))

}


func (wsh wsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

    // upgrader is needed to upgrade the HTTP Connection to a websocket Connection

        upgrader := &websocket.Upgrader{

            ReadBufferSize:  1024,

            WriteBufferSize: 1024,

        }


        //Upgrading HTTP Connection to websocket connection

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

        if err != nil {

            log.Printf("error upgrading %s", err)

            return

        }


        //handle your websockets with wsConn

}

在您的 Javascript 中,您var sock = new WebSocket("ws://localhost/ws:8080");顯然需要


查看完整回答
反對 回復(fù) 2021-12-07
  • 1 回答
  • 0 關(guān)注
  • 414 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)