Discord error: Connection closed by server. Code: 4002, Reason: Error while decoding payload

For clarity, this is a custom Discord bot where I am coding everything from scratch and not using a library because the environment I am working in is not capable of accessing libraries.

So far, everything has been working (mostly) wonderfully and I can

  • connect using HTTP protocol, I am able to upgrade to WebSockets protocol
  • send Identify
  • send heartbeat
  • receive heartbeat acknowledgement

So, I can see, probably, that I am correctly masking and framing my outgoing payloads, or else my Identify and heartbeat would fail; Identify being a WS_TEXT_FRAME.

I am testing my bot and when it sees a message, which it does, it is supposed to just reply “Hello, [name]!” in the channel it received it. So, unmasking and parsing frames is working as well.

When I send my message, Discord disconnects me with the message Connection closed by server. Code: 4002, Reason: Error while decoding payload.

I am tracking sequence number with every WS_TEXT_FRAME i receive and am including the most recent one in my payload I am sending back.

I believe my payload is correct. I have tested masking it and also unmasking it and it looks good to me.

{"op":0,"t":"MESSAGE_CREATE","d":{"content":"Hello, karahd!","channel_id":"675155041849573378"},"s":3}

Here are some (overly enthusiastic) logs of what is happening.

(Discord) [/adm/daemons/discord.c:handle_event:371] Handing event MESSAGE_CREATE to bot Chatter
(Chatter) Message from karahd: hi there
(Discord) [/std/daemon/websocket_client.c:socket_read:357] Final buffer size: 0
(Discord) [/std/daemon/websocket_client.c:socket_read:358] ===========  ENDING WS TRANSACTION 5  ===========
(Discord) [/adm/daemons/discord.c:query_bot:561] Handle: ([ /* sizeof() == 9 */
  "session_id" : "8d19ad0d4e250def57da6a6a7e452774",
  "heartbeat" : 325948,
  "bot" : /adm/daemons/modules/discord/chatter ("/adm/daemons/modules/discord/chatter"),
  "host" : "gateway.discord.gg",
  "seq" : 3,
  "port" : 443,
  "heartbeat_seq" : 0,
  "ready" : 1,
  "heartbeat_interval" : 41.250000,
])
(Discord) [/adm/daemons/discord.c:query_bot:565] Bot: /adm/daemons/modules/discord/chatter ("/adm/daemons/modules/discord/chatter")
(Discord) [/std/daemon/websocket_client.c:send_message:846] Preparing to format frame with opcode: 1 and payload: ({ /* sizeof() == 1 */
  "{"op":0,"t":"MESSAGE_CREATE","d":{"content":"Hello, karahd!","channel_id":"675155041849573378"},"s":3}"
})
(Discord) [/std/daemon/websocket_client.c:format_frame:724] Payload length: 102
(Discord) [/std/daemon/websocket_client.c:format_frame:725] Masking key: "0xed 0xc9 0xbe 0x3b"
(Discord) [/std/daemon/websocket_client.c:format_frame:726] Payload before masking: "0x7b 0x22 0x6f 0x70 0x22 0x3a 0x30 0x2c 0x22 0x74 0x22 0x3a 0x22 0x4d 0x45 0x53 0x53 0x41 0x47 0x45 0x5f 0x43 0x52 0x45 0x41 0x54 0x45 0x22 0x2c 0x22 0x64 0x22 0x3a 0x7b 0x22 0x63 0x6f 0x6e 0x74 0x65 0x6e 0x74 0x22 0x3a 0x22 0x48 0x65 0x6c 0x6c 0x6f 0x2c 0x20 0x6b 0x61 0x72 0x61 0x68 0x64 0x21 0x22 0x2c 0x22 0x63 0x68 0x61 0x6e 0x6e 0x65 0x6c 0x5f 0x69 0x64 0x22 0x3a 0x22 0x36 0x37 0x35 0x31 0x35 0x35 0x30 0x34 0x31 0x38 0x34 0x39 0x35 0x37 0x33 0x33 0x37 0x38 0x22 0x7d 0x2c 0x22 0x73 0x22 0x3a 0x33 0x7d"
(Discord) [/std/daemon/websocket_client.c:format_frame:758] Payload length: 102
(Discord) [/std/daemon/websocket_client.c:format_frame:759] Masking key: "0xed 0xc9 0xbe 0x3b"
(Discord) [/std/daemon/websocket_client.c:format_frame:760] Payload before masking: "0x7b 0x22 0x6f 0x70 0x22 0x3a 0x30 0x2c 0x22 0x74 0x22 0x3a 0x22 0x4d 0x45 0x53 0x53 0x41 0x47 0x45 0x5f 0x43 0x52 0x45 0x41 0x54 0x45 0x22 0x2c 0x22 0x64 0x22 0x3a 0x7b 0x22 0x63 0x6f 0x6e 0x74 0x65 0x6e 0x74 0x22 0x3a 0x22 0x48 0x65 0x6c 0x6c 0x6f 0x2c 0x20 0x6b 0x61 0x72 0x61 0x68 0x64 0x21 0x22 0x2c 0x22 0x63 0x68 0x61 0x6e 0x6e 0x65 0x6c 0x5f 0x69 0x64 0x22 0x3a 0x22 0x36 0x37 0x35 0x31 0x35 0x35 0x30 0x34 0x31 0x38 0x34 0x39 0x35 0x37 0x33 0x33 0x37 0x38 0x22 0x7d 0x2c 0x22 0x73 0x22 0x3a 0x33 0x7d"
(Discord) [/std/daemon/websocket_client.c:format_frame:761] Masked payload: "0x96 0xeb 0xd1 0x4b 0xcf 0xf3 0x8e 0x17 0xcf 0xbd 0x9c 0x01 0xcf 0x84 0xfb 0x68 0xbe 0x88 0xf9 0x7e 0xb2 0x8a 0xec 0x7e 0xac 0x9d 0xfb 0x19 0xc1 0xeb 0xda 0x19 0xd7 0xb2 0x9c 0x58 0x82 0xa7 0xca 0x5e 0x83 0xbd 0x9c 0x01 0xcf 0x81 0xdb 0x57 0x81 0xa6 0x92 0x1b 0x86 0xa8 0xcc 0x5a 0x85 0xad 0x9f 0x19 0xc1 0xeb 0xdd 0x53 0x8c 0xa7 0xd0 0x5e 0x81 0x96 0xd7 0x5f 0xcf 0xf3 0x9c 0x0d 0xda 0xfc 0x8f 0x0e 0xd8 0xf9 0x8a 0x0a 0xd5 0xfd 0x87 0x0e 0xda 0xfa 0x8d 0x0c 0xd5 0xeb 0xc3 0x17 0xcf 0xba 0x9c 0x01 0xde 0xb4"
(Discord) [/std/daemon/websocket_client.c:format_frame:762] Final frame: "0x81 0xe6 0xed 0xc9 0xbe 0x3b 0x96 0xeb 0xd1 0x4b 0xcf 0xf3 0x8e 0x17 0xcf 0xbd 0x9c 0x01 0xcf 0x84 0xfb 0x68 0xbe 0x88 0xf9 0x7e 0xb2 0x8a 0xec 0x7e 0xac 0x9d 0xfb 0x19 0xc1 0xeb 0xda 0x19 0xd7 0xb2 0x9c 0x58 0x82 0xa7 0xca 0x5e 0x83 0xbd 0x9c 0x01 0xcf 0x81 0xdb 0x57 0x81 0xa6 0x92 0x1b 0x86 0xa8 0xcc 0x5a 0x85 0xad 0x9f 0x19 0xc1 0xeb 0xdd 0x53 0x8c 0xa7 0xd0 0x5e 0x81 0x96 0xd7 0x5f 0xcf 0xf3 0x9c 0x0d 0xda 0xfc 0x8f 0x0e 0xd8 0xf9 0x8a 0x0a 0xd5 0xfd 0x87 0x0e 0xda 0xfa 0x8d 0x0c 0xd5 0xeb 0xc3 0x17 0xcf 0xba 0x9c 0x01 0xde 0xb4"
(Discord) [/std/daemon/websocket_client.c:send_message:853] Frame type: 1, Message: ({ /* sizeof() == 1 */
  "{"op":0,"t":"MESSAGE_CREATE","d":{"content":"Hello, karahd!","channel_id":"675155041849573378"},"s":3}"
})
(Discord) [/std/daemon/websocket_client.c:send_message:854] Binary frame: "0x81 0xe6 0xed 0xc9 0xbe 0x3b 0x96 0xeb 0xd1 0x4b 0xcf 0xf3 0x8e 0x17 0xcf 0xbd 0x9c 0x01 0xcf 0x84 0xfb 0x68 0xbe 0x88 0xf9 0x7e 0xb2 0x8a 0xec 0x7e 0xac 0x9d 0xfb 0x19 0xc1 0xeb 0xda 0x19 0xd7 0xb2 0x9c 0x58 0x82 0xa7 0xca 0x5e 0x83 0xbd 0x9c 0x01 0xcf 0x81 0xdb 0x57 0x81 0xa6 0x92 0x1b 0x86 0xa8 0xcc 0x5a 0x85 0xad 0x9f 0x19 0xc1 0xeb 0xdd 0x53 0x8c 0xa7 0xd0 0x5e 0x81 0x96 0xd7 0x5f 0xcf 0xf3 0x9c 0x0d 0xda 0xfc 0x8f 0x0e 0xd8 0xf9 0x8a 0x0a 0xd5 0xfd 0x87 0x0e 0xda 0xfa 0x8d 0x0c 0xd5 0xeb 0xc3 0x17 0xcf 0xba 0x9c 0x01 0xde 0xb4"
(Discord) [/std/daemon/websocket_client.c:send_message:857] Unformatted frame: {"op":0,"t":"MESSAGE_CREATE","d":{"content":"Hello, karahd!","channel_id":"675155041849573378"},"s":3}
(Discord) [/std/daemon/websocket_client.c:send_message:864] Sent message to gateway.discord.gg
(Discord) [/std/daemon/websocket_client.c:socket_read:267] ===========  STARTING WS TRANSACTION 6  ===========
(Discord) [/std/daemon/websocket_client.c:socket_read:278] Incoming size: 33
(Discord) [/std/daemon/websocket_client.c:socket_read:279] Buffer size: 33
(Discord) [/std/daemon/websocket_client.c:socket_read:331] Processing WebSocket data
(Discord) [/std/daemon/websocket_client.c:is_message_complete:475] Enough data for the full payload
(Discord) [/std/daemon/websocket_client.c:parse_websocket_frame:496] Initial frame details - fin: 128, opcode: 8, masked: 0, payload_length: 31
(Discord) [/std/daemon/websocket_client.c:parse_websocket_frame:517] Processed payload length: 31, offset: 2
(Discord) [/std/daemon/websocket_client.c:parse_websocket_frame:538] Payload unmasked or copied
(Discord) [/std/daemon/websocket_client.c:parse_websocket_frame:552] Offset: 2, Payload Length: 31, Buffer Size: 33
(Discord) [/std/daemon/websocket_client.c:parse_websocket_frame:553] Remaining buffer size: 0
(Discord) [/std/daemon/websocket_client.c:process_websocket_message:586] Received close frame
(Discord) [/std/daemon/websocket_client.c:process_websocket_message:587] Payload: "0x0f 0xa2 0x45 0x72 0x72 0x6f 0x72 0x20 0x77 0x68 0x69 0x6c 0x65 0x20 0x64 0x65 0x63 0x6f 0x64 0x69 0x6e 0x67 0x20 0x70 0x61 0x79 0x6c 0x6f 0x61 0x64 0x2e"
(Discord) [/std/daemon/websocket_client.c:process_close_frame:632] Connection closed by server. Code: 4002, Reason: Error while decoding payload.
(Discord) [/adm/daemons/discord.c:query_bot:561] Handle: ([ /* sizeof() == 9 */
  "session_id" : "8d19ad0d4e250def57da6a6a7e452774",
  "heartbeat" : 325948,
  "bot" : /adm/daemons/modules/discord/chatter ("/adm/daemons/modules/discord/chatter"),
  "host" : "gateway.discord.gg",
  "seq" : 3,
  "port" : 443,
  "heartbeat_seq" : 0,
  "ready" : 1,
  "heartbeat_interval" : 41.250000,
])
(Discord) [/adm/daemons/discord.c:query_bot:565] Bot: /adm/daemons/modules/discord/chatter ("/adm/daemons/modules/discord/chatter")
(Discord) [/adm/daemons/discord.c:query_bot:561] Handle: ([ /* sizeof() == 9 */
  "session_id" : "8d19ad0d4e250def57da6a6a7e452774",
  "heartbeat" : 325948,
  "bot" : /adm/daemons/modules/discord/chatter ("/adm/daemons/modules/discord/chatter"),
  "host" : "gateway.discord.gg",
  "seq" : 3,
  "port" : 443,
  "heartbeat_seq" : 0,
  "ready" : 1,
  "heartbeat_interval" : 41.250000,
])
(Discord) [/adm/daemons/discord.c:query_bot:565] Bot: /adm/daemons/modules/discord/chatter ("/adm/daemons/modules/discord/chatter")
(Discord) [/adm/daemons/discord.c:handle_close_frame:256] Received close frame
(Discord) [/adm/daemons/discord.c:handle_close_frame:260] Bot Chatter has been disconnected
(Discord) [/std/daemon/websocket_client.c:socket_read:357] Final buffer size: 0
(Discord) [/std/daemon/websocket_client.c:socket_read:358] ===========  ENDING WS TRANSACTION 6  ===========
(Discord) [/std/daemon/websocket_client.c:socket_closed:200] Socket closed for fd 0
(Discord) [/std/daemon/websocket_client.c:socket_closed:205] Socket closed: gateway.discord.gg 443
(Discord) [/std/daemon/websocket_client.c:shutdown_socket:220] Shutting down socket for 0
(Discord) [/std/daemon/websocket_client.c:shutdown_socket:233] Total data sent: 470 bytes
(Discord) [/std/daemon/websocket_client.c:shutdown_socket:234] Total transactions: 6
(Discord) [/std/daemon/websocket_client.c:shutdown_socket:235] Total data received: 16231 bytes
(Discord) [/std/daemon/websocket_client.c:shutdown_socket:236] Uptime: 5.29 seconds
(Discord) [/std/daemon/websocket_client.c:shutdown_socket:239] Handling shutdown for 0
(Discord) [/adm/daemons/discord.c:handle_shutdown:218] Handling shutdown as requested
(Discord) [/adm/daemons/discord.c:handle_shutdown:236] Stopping heartbeat
(Chatter) Chatter bot has been shut down
(Discord) [/std/daemon/websocket_client.c:shutdown_socket:249] Socket removed: gateway.discord.gg 443

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật