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

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

Websocket handshake fails 404 (golang server)

问题

我有一个简单的Go Web服务器,它在localhost:8080端口上提供一个包含HTML文件和客户端脚本(带有WebSocket逻辑)的public文件夹。

在我的main.go文件中:

  1. listener, err := net.listen("tcp", "localhost:8080")
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. // 完整的代码在此处:https://gist.github.com/Kielan/98706aaf5dc0be9d6fbe

然后在我的客户端脚本中:

  1. try {
  2. var sock = new WebSocket("ws://127.0.0.1:8080");
  3. console.log("Websocket - status: " + sock.readyState);
  4. sock.onopen = function(message) {
  5. console.log("CONNECTION opened..." + this.readyState);
  6. // onmessage, onerr, onclose, 等等...
  7. }
  8. }

我在Chrome中遇到了以下错误:

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

以及在Firefox中:

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

我在这篇文章中找到了关于Node.js的信息,它指出需要在客户端的WebSocket字符串中添加/websocket,但这并没有解决问题,反而导致了404错误。

我原以为响应代码200是好的,难道我需要以某种方式将请求转换为WebSocket,并且可能默认为HTTP吗?如果是这样,我应该如何做到这一点?

英文:

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

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

then in my client script

  1. try {
  2. var sock = new WebSocket("ws://127.0.0.1:8080");
  3. console.log("Websocket - status: " + sock.readyState);
  4. sock.onopen = function(message) {
  5. console.log("CONNECTION opened..." + this.readyState);
  6. //onmessage, onerr, onclose, ect...
  7. }

I get the error in chrome

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

and Firefox

  1. 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?

答案1

得分: 1

像JimB指出的那样,你还没有处理HTTP和WebSocket连接。

你可以使用github.com/gorilla/websocket包来处理WebSocket连接。一个简单的设置如下所示:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "github.com/gorilla/websocket"
  6. )
  7. // wsHandler 实现了 Handler 接口
  8. type wsHandler struct{}
  9. func main() {
  10. router := http.NewServeMux()
  11. router.Handle("/", http.FileServer(http.Dir("./webroot"))) // 处理位于 ./webroot 下的静态 HTML / CSS 等文件
  12. router.Handle("/ws", wsHandler{}) // 处理 WebSocket 连接
  13. // 启动服务
  14. log.Fatal(http.ListenAndServe("localhost:8080", router))
  15. }
  16. func (wsh wsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  17. // upgrader 用于将 HTTP 连接升级为 WebSocket 连接
  18. upgrader := &websocket.Upgrader{
  19. ReadBufferSize: 1024,
  20. WriteBufferSize: 1024,
  21. }
  22. // 将 HTTP 连接升级为 WebSocket 连接
  23. wsConn, err := upgrader.Upgrade(w, r, nil)
  24. if err != nil {
  25. log.Printf("升级连接时出错:%s", err)
  26. return
  27. }
  28. // 使用 wsConn 处理你的 WebSocket
  29. }

然后在你的 JavaScript 中,你需要使用 var sock = new WebSocket("ws://localhost/ws:8080");

英文:

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:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "github.com/gorilla/websocket"
  6. )
  7. // wsHandler implements the Handler Interface
  8. type wsHandler struct{}
  9. func main() {
  10. router := http.NewServeMux()
  11. router.Handle("/", http.FileServer(http.Dir("./webroot"))) //handles static html / css etc. under ./webroot
  12. router.Handle("/ws", wsHandler{}) //handels websocket connections
  13. //serving
  14. log.Fatal(http.ListenAndServe("localhost:8080", router))
  15. }
  16. func (wsh wsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  17. // upgrader is needed to upgrade the HTTP Connection to a websocket Connection
  18. upgrader := &websocket.Upgrader{
  19. ReadBufferSize: 1024,
  20. WriteBufferSize: 1024,
  21. }
  22. //Upgrading HTTP Connection to websocket connection
  23. wsConn, err := upgrader.Upgrade(w, r, nil)
  24. if err != nil {
  25. log.Printf("error upgrading %s", err)
  26. return
  27. }
  28. //handle your websockets with wsConn
  29. }

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

huangapple
  • 本文由 发表于 2016年2月4日 21:32:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/35202405.html
匿名

发表评论

匿名网友

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

确定