ralscha / sse-eventbus

EventBus library for sending events from a Spring appliction to the web browser with SSE
Apache License 2.0
83 stars 28 forks source link

request:add object converters in configurer #7

Closed smithtonson closed 6 years ago

smithtonson commented 6 years ago

The SseEventBusConfigurer doesn't have support for adding object converters, so I have to subclass the main config file but that's a problem due to private members

ralscha commented 6 years ago

You don't have to subclass the main config file. You can add @Bean methods that return a DataObjectConverter

@SpringBootApplication
@EnableSseEventBus
public class TestDefaultConfiguration implements SseEventBusConfigurer {
    @Override
    public Duration clientExpiration() {
        return Duration.ofSeconds(10);
    }

    @Bean
    public DataObjectConverter testObject2Converter() {
        return new DataObjectConverter() {

            @Override
            public boolean supports(SseEvent event) {
                return event.data() instanceof TestObject2;
            }

            @Override
            public String convert(SseEvent event) {
                TestObject2 to = (TestObject2) event.data();
                return to.getId() + "," + to.getCustomer();
            }
        };
    }
}
smithtonson commented 6 years ago

Ah, gotcha; that's working for me. Thanks