Websocket握手失败404(golang服务器)

我有一个简单的Go Web服务器,该服务器在localhost:8080端口上提供了一个同时包含html文件的公用文件夹 作为具有websocket逻辑的客户端脚本。</ p>

在我的 main.go文件中 </ p>

  listener,错误:= net.listen(“ tcp”,“ localhost:8080”)
if错误!= nil {
log.Fatal( err)
}
//要点中的完整代码https://gist.github.com/Kielan/98706aaf5dc0be9d6fbe
</ code> </ pre>

然后在我的客户端脚本中</ p>

  try {
var sock = new WebSocket(“ ws://127.0.0.1:8080”);
console.log(“ Websocket-status:” + sock。 readyState);

sock.onopen = function(message){
console.log(“ CONNECTION open ...” + this.readyState);
// onmessage,onerr,onclose,ect ... \ n}
</ code> </ pre>

我收到了chrome中的错误</ p>

  WebSocket连接到'ws://127.0.0.1  :8080 /'失败:WebSocket握手期间出错:意外 响应代码:200 
</ code> </ pre>

和Firefox </ p>

  Firefox无法在ws建立与服务器的连接:  //127.0.0.1:8080 /。
</ code> </ pre>

我发现本文涉及到node.js,指示将/ websocket添加到我的客户端websocket字符串中,尽管它不能解决问题并导致404 < / p>

我认为响应代码200不错,是否需要以某种方式将请求转换为websocket,也许默认为http? 如果可以,我该怎么做?</ p>
</ div>

展开原文

原文

I have a simple go web server which serves on port localhost:8080 an public folder containing both an html file as well as a client script with websocket logic.

in my main.go file

listener, err := net.listen("tcp", "localhost:8080")
if err != nil {
    log.Fatal(err)
}
//full code in gist https://gist.github.com/Kielan/98706aaf5dc0be9d6fbe

then in my client script

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...
}

I get the error in chrome

WebSocket connection to 'ws://127.0.0.1:8080/' failed: Error during WebSocket handshake: Unexpected response code: 200

and Firefox

Firefox can't establish a connection to the server at ws://127.0.0.1:8080/.

I found this article referring to node.js indicating to add /websocket to my client websocket string, though it did not solve the problem and resulted in a 404

I thought response code 200 is good, do I need to convert the request to a websocket somehow and maybe it is defaulting to http? If so how can I do this?

duanlang1531
duanlang1531 您的代码仅在tcp套接字上侦听。您根本没有http或websocket处理代码。Websocket是HTTP/1.1的一部分en.wikipedia.org/wiki/WebSocket
4 年多之前 回复
dongpian2559
dongpian2559 将要点添加到main.go文件中,感谢您的反馈。
4 年多之前 回复
dongliu8542
dongliu8542 服务器端(或http服务器的任何部分)上的Web套接字处理代码在哪里?
4 年多之前 回复

1个回答

Like JimB pointed out, you are not handling http nor websocket connections yet.

You can do websocket handling with the package github.com/gorilla/websocket This is how a simple setup could look like:

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
}

In your Javascript you then need var sock = new WebSocket("ws://localhost/ws:8080"); obviously.

douxiong5438
douxiong5438 如果您想查看更多代码,也可以看看我编写的使用大猩猩包的井字游戏服务器。 github.com/riscie/websocket-tic-tac-toe
4 年多之前 回复
dongshao6591
dongshao6591 谢谢,在我研究解决方案时,我意识到使用大猩猩可能是最好的解决方案。 我还要补充一点,这是仓库中一个很好的聊天示例。 github.com/gorilla/websocket/tree/master/examples/chat
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐