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

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

    点赞 评论

相关推荐