Reputation: 341
I cloned the gorilla websocket chat example and updated it to use multiple rooms. However, I am getting the error:
Error during WebSocket handshake: Unexpected response code: 404
In chrome whenever I try to make the connection. My source code is available on github. It is very similar to the original example, and was only changed slightly. I don't know why it is not working.
EDIT: The problem is occuring in this line of code:
for _, name := range []string{"arduino", "java", "go", "scala"} {
room := newRoom("go")
http.Handle("/chat/go", room)
go room.run()
}
Looping over a slice is causing an issue with the httphandle function. If instead, I declare them individually:
room := newRoom("go")
http.Handle("/chat/go", room)
go room.run()
...
It works. How can I fix this?
Upvotes: 0
Views: 696
Reputation: 828
So actually from your index.html
file, you connect to wrong 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
This is your url from main.go
http.Handle("/chat"+name, room)
It will make url like this: http://localhost:8080/chatgo, rather than what you want: http://localhost:8080/chat/go
Fyi, it will error because you don't properly handle the channel
, so after I send 1 message, it will be automatically closed. But this is another topic.
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
Upvotes: 1