douxuanjie2692 2018-04-07 05:11
浏览 24
已采纳

客户端重新加载页面时如何解决服务器故障?

When the client reloads the page, the server issues an error - websocket: close 1001 (going away). When the client reloads the page, does not the websocket connection reconnect? I recently started studying programming, so please forgive if the question is stupid. Client code

var socket = new WebSocket("ws://localhost:8080/ws" );

socket.onmessage = function (event) {
    DownloadScene(event)
}

function DownloadScene(event) {
    var data = JSON.parse(event.data)
    document.getElementById(data.id).innerHTML = data.body;
}
function  loadScene(scene) {
  var page = {
    query_type:"loadScene",
    data : {temp:scene}
}
   var event = JSON.stringify(page);
   socket.send(event);
}

I'm using the gorilla websocket library to build a connection.

Server code

func WebSocketHandler(w http.ResponseWriter, r *http.Request) {

   conn, err := upGrader.Upgrade(w, r, nil)

   if err != nil {
      log.Println(err)
      return
   }
   for { webSocketHandler(conn)}
   defer conn.Close()
}

func webSocketHandler(conn *websocket.Conn) {

  println("new connect")
  err := conn.ReadJSON(&query)
  if err != nil {
      log.Fatal(err)
      return
  }
  switch query.QueryType {
  case "loadScene" :
    err := json.Unmarshal(query.Data,&frames)
    if err!= nil{
        log.Fatal(err)
    }
    var buf  bytes.Buffer
    data := frame.ExecuteTemplate(&buf,"frame",nil)
    if data!=nil{
        log.Fatal(data)
    }
    res,e := json.Marshal(&Frame{"scene",buf.String()})
    if e !=nil{
        log.Println(e)
    }

    errs := conn.WriteMessage(1,res)
    if err!=nil{
        log.Fatal(errs)
    }
 }
}
  • 写回答

2条回答 默认 最新

  • drfals1307 2018-04-07 05:41
    关注

    In go, log.Fatal(..) exits your application.

    err := conn.ReadJSON(&query)
    

    will return an err when the client connection closes and the next block:

    if err != nil {
      log.Fatal(err)
      return
    }
    

    will close the go server and the Javascript client will be unable to reconnect.

    Your server structure also doesn't look quite correct - WebSocketHandler is invoked to handle a single connection:

        .
        http.HandleFunc("/", WebSocketHandler)
        log.Fatal(http.ListenAndServe("localhost:8080", nil))
    }
    
    func WebSocketHandler(w http.ResponseWriter, r *http.Request) {
        conn, err := upgrader.Upgrade(w, r, nil)
        if err != nil {
            log.Print("Error upgrading websocket:", err)
            return
        }
    
        defer conn.Close()
    
        for {
            err = conn.ReadJSON(&query)
            if err != nil {
                log.Print("Error reading query: ", err)
                return
            }
            .
            // -- process query here --
            .
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测