braverhealth / phoenix-socket-dart

Cross-platform and stream-based implementation of Phoenix Sockets
https://pub.dev/packages/phoenix_socket
BSD 3-Clause "New" or "Revised" License
73 stars 37 forks source link

Add support for custom decoder and encoder #59

Closed rsmything closed 1 year ago

rsmything commented 1 year ago

Adding support to add your own encoder and decoder.

For example:

Using BERT decoder for incoming messages https://pub.dev/packages/bert https://github.com/Youimmi/bert.git

import 'package:bert/bert.dart as bert';
import 'package:phoenix_socket/src/message_serializer.dart';

socket = PhoenixSocket(
  socketUrl,
  socketOptions: PhoenixSocketOptions(
    serializer: MessageSerializer(decoder: bert.decode),
  ),
);
matehat commented 1 year ago

Thanks for this!

I'm a little hesitant to loosen the typing of the incoming messages into dynamic because they could be either String or List<int>. What do you think of changing the class hierarchy a bit so that there could be a PhoenixRawSocket of something like that, that expects List<int> and another that expects String?

rsmything commented 1 year ago

Thank you for reply, i will rewrite it soon.