亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Gorilla Websocket:WebSocket 握手期間出錯:意外響應代碼:404

Gorilla Websocket:WebSocket 握手期間出錯:意外響應代碼:404

Go
慕絲7291255 2022-06-21 16:12:48
我克隆了 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()...有用。我怎樣才能解決這個問題?
查看完整描述

1 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

所以實際上從你的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

這是您的網址main.go


    http.Handle("/chat"+name, room)

它會使 url 像這樣:http://localhost:8080/chatgo,而不是你想要的:http://localhost:8080/chat/go


Fyi,它會因為你沒有正確處理而出錯channel,所以在我發送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


查看完整回答
反對 回復 2022-06-21
  • 1 回答
  • 0 關注
  • 401 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號