【发布时间】:2020-11-24 00:45:23
【问题描述】:
我克隆了 gorilla websocket 聊天示例并将其更新为使用多个房间。但是,我收到错误:
Error during WebSocket handshake: Unexpected response code: 404
每当我尝试建立连接时,都在 chrome 中。我的源代码在github 上可用。它与原始示例非常相似,只是略有改动。我不知道为什么它不起作用。
编辑: 问题出现在这行代码中:
for _, name := range []string{"arduino", "java", "go", "scala"} {
room := newRoom("go")
http.Handle("/chat/go", room)
go room.run()
}
循环切片会导致 httphandle 函数出现问题。相反,我单独声明它们:
room := newRoom("go")
http.Handle("/chat/go", room)
go room.run()
...
它有效。我该如何解决这个问题?
【问题讨论】:
-
嗨,我检查了你的代码,它可以工作ibb.co/y023X26。您的操作系统和 Golang 版本是什么?
-
@FahimBagar 我认为您获取了错误的链接。它在“房间”分支下
标签: go websocket http-status-code-404 handshake gorilla