Skip to content

Commit

Permalink
avoid extra allocs in serializing commands, update example
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Jan 25, 2025
1 parent deabc29 commit 77b6582
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
10 changes: 8 additions & 2 deletions example/console/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ void main() async {
final channel = 'chat:index';
// generate user JWT token for user "dart":
// ./centrifugo gentoken --user dart
// For this example we generated token using HMAC secret key "secret". Don't forget
// that it's just an example, and tokens never should be generated on client side and
// HMAC secret revealed to client side.
final userJwtToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkYXJ0IiwiZXhwIjoyMjc5NDQzNjgwLCJpYXQiOjE2NzQ2NDM2ODB9.XgsPZzAD4kMj7HdybJfpMGuDaRmuLvhUUxCGHs3mtXA';
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkYXJ0IiwiaWF0IjoxNzM3ODAwOTcxfQ.m30QzH9nxqMSJw3g5gL5Tjqnu-veQSn4RcH47ZozqQI';
// generate subscription JWT token for user "dart" and channel "chat:index":
// ./centrifugo gensubtoken --user dart --channel chat:index
// For this example we generated subscription token using HMAC secret key "secret". Don't forget
// that it's just an example, and tokens never should be generated on client side and
// HMAC secret revealed to client side.
final subscriptionJwtToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkYXJ0IiwiZXhwIjoyMjc5NDQ0MDE4LCJpYXQiOjE2NzQ2NDQwMTgsImNoYW5uZWwiOiJjaGF0OmluZGV4In0.FjpnF6ofq3XCr1iqnwTZcpxCx6btuzCnn29DAIJbsBo';
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkYXJ0IiwiaWF0IjoxNzM3ODAxMDIwLCJjaGFubmVsIjoiY2hhdDppbmRleCJ9.fe3WguZKkGiTyacpUW-WJW-1yzpZTHAFO6FWo7gbbTs';

final onEvent = (dynamic event) {
print('client> $event');
Expand Down
9 changes: 4 additions & 5 deletions lib/src/codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ class ProtobufCommandEncoder extends CommandEncoder {
@override
List<int> convert(Command input) {
final commandData = input.writeToBuffer();
final length = commandData.lengthInBytes;

final writer = CodedBufferWriter();
writer.writeInt32NoTag(length);

return writer.toBuffer() + commandData;
return (CodedBufferWriter()
..writeInt32NoTag(commandData.length)
..writeRawBytes(commandData))
.toBuffer();
}
}

Expand Down

0 comments on commit 77b6582

Please sign in to comment.