encodeClosePayload function
The payload of a close frame: the code, then the reason in UTF-8, or nothing at all when there is no code.
Implementation
Uint8List encodeClosePayload(int? code, String reason) {
if (code == null) return Uint8List(0);
final text = utf8.encode(reason);
final payload = Uint8List(2 + text.length);
payload[0] = code >> 8;
payload[1] = code & 0xff;
payload.setRange(2, payload.length, text);
return payload;
}