Gorilla Websocket:Websocket握手时出错:意外响应代码:404

Gorilla Websocket:Websocket握手时出错:意外响应代码:404,go,websocket,http-status-code-404,handshake,gorilla,Go,Websocket,Http Status Code 404,Handshake,Gorilla,我克隆了gorilla websocket聊天示例,并将其更新为使用多个聊天室。但是,我得到了一个错误: WebSocket握手时出错:意外响应代码:404 在chrome中,每当我尝试建立连接时。我的源代码可以在上找到。它与原来的示例非常相似,只是做了轻微的更改。我不知道为什么它不起作用 编辑: 此代码行中出现问题: for _, name := range []string{"arduino", "java", "go", &quo

我克隆了gorilla websocket聊天示例,并将其更新为使用多个聊天室。但是,我得到了一个错误: WebSocket握手时出错:意外响应代码:404 在chrome中,每当我尝试建立连接时。我的源代码可以在上找到。它与原来的示例非常相似,只是做了轻微的更改。我不知道为什么它不起作用

编辑: 此代码行中出现问题:

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()
...

它起作用了。如何修复此问题?

因此,实际上从
index.html
文件中,您连接到了错误的url

<!-- index.html -->
<script>
    var serviceLocation = "ws://0.0.0.0:8080/chat/";
.....

    function connectToChatserver() {
        room = $('#chatroom option:selected').val();
        wsocket = new WebSocket(serviceLocation + room);
        // it connect to /chat/<room>, it has slash after chat
它将使url如下所示:http://localhost:8080/chatgo,而不是您想要的:http://localhost:8080/chat/go

仅供参考,它将出错,因为您没有正确处理
频道
,因此在我发送1条消息后,它将自动关闭。但这是另一个话题

2020/08/04 06:42:10 running chat room java
2020/08/04 06:42:10 running chat room go
2020/08/04 06:42:10 running chat room arduino
2020/08/04 06:42:10 running chat room scala
2020/08/04 06:42:15 new client in room arduino
2020/08/04 06:42:15 client leaving room arduino
2020/08/04 06:42:15 client leaving room arduino
panic: close of closed channel

goroutine 6 [running]:
main.(*Room).run(0xc00007ac90)
        /home/fahim/Projects/Golang/go-chat/room.go:70 +0x3b5
created by main.main
        /home/fahim/Projects/Golang/go-chat/main.go:17 +0x2bd
exit status 2

嗨,我已经检查过你的代码了。你的操作系统和你的Golang版本是什么?@FahimBagar我想你搞错链接了。这是下分店的“房间”谢谢!这就解决了问题。我知道这是一件很愚蠢的事:)。我修复了通道关闭错误。谢谢
2020/08/04 06:42:10 running chat room java
2020/08/04 06:42:10 running chat room go
2020/08/04 06:42:10 running chat room arduino
2020/08/04 06:42:10 running chat room scala
2020/08/04 06:42:15 new client in room arduino
2020/08/04 06:42:15 client leaving room arduino
2020/08/04 06:42:15 client leaving room arduino
panic: close of closed channel

goroutine 6 [running]:
main.(*Room).run(0xc00007ac90)
        /home/fahim/Projects/Golang/go-chat/room.go:70 +0x3b5
created by main.main
        /home/fahim/Projects/Golang/go-chat/main.go:17 +0x2bd
exit status 2