dsfjk44656 2016-12-09 22:27
浏览 214
已采纳

WebSocket在WriteMessage上失败

I'm building out a simple Slackbot as a learning experience with Go, and I've hit my first snag: I'm unable to write a message back to the connection!

Here's my main func:

func main() {
  conn, botId, err := slackInit(os.Getenv("SLACKBOT_TOKEN"))
  if err != nil { return }

  defer conn.Close()
  for {
    _, event, err := conn.ReadMessage()
    if err != nil {
      fmt.Println("Error processing message:", err)
      return
    }

    fmt.Println(string(event))

    message, err := slackGetMessage(event)
    if strings.Contains(message, botId) {
      fmt.Println("Bot was mentioned!")
      resp := []byte("You talkin' to me?")
      err = conn.WriteMessage(1, resp)
      if err != nil {
        fmt.Println("Error writing message:", string(resp))
        return
      }
    }
  }
}

This listens and reports events with no issue, but fails as soon as the conn.WriteMessage method is called. I get a fatal error from websocket with code 1006 & the message "unexpected closure".

I'm using Gorilla for my websocket library, and I suspect maybe this is a concurrency issue related to their "one reader, one writer" limit. I've tried a few tweaks, but honestly just don't know enough about the languge/library yet to really dive deep on debugging this. :-/

I'm sure I'm missing something obvious here! Any tips for managing my Writer to get my bot talking back? Thanks!

  • 写回答

1条回答 默认 最新

  • doushi7819 2016-12-10 00:42
    关注

    The message sent to the server is not JSON as expected by the server. The server closed the connection without a handshake.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法