diff --git a/src/main/java/ch/push/notification/DataEmitterService.java b/src/main/java/ch/push/notification/DataEmitterService.java index 7b0ca12..ee5893c 100644 --- a/src/main/java/ch/push/notification/DataEmitterService.java +++ b/src/main/java/ch/push/notification/DataEmitterService.java @@ -1,8 +1,9 @@ -package ch.rasc; +package ch.push.notification; import java.util.ArrayList; import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -13,16 +14,12 @@ @Service public class DataEmitterService { - private final ApplicationEventPublisher eventPublisher; - // OR: private final ApplicationContext ctx; - // this class implements the ApplicationEventPublisher interface private List list = new ArrayList(); //private final static Random random = new Random(); - public DataEmitterService(ApplicationEventPublisher eventPublisher) { - this.eventPublisher = eventPublisher; - } + @Autowired + private ApplicationEventPublisher eventPublisher; public void addListInfo(String id) { @@ -47,17 +44,18 @@ public void sendData() { if(list.indexOf(str) == 0) { - Builder builder1 = new SseEvent.Builder(); + + /*Builder builder1 = new SseEvent.Builder(); Builder builder = builder1.from(SseEvent.ofData("Delhi")); - builder.addClientId(str); - this.eventPublisher.publishEvent(builder.build()); + builder.addClientId(str);*/ + this.eventPublisher.publishEvent(SseEvent.builder().addClientId(str).event("DataOnline").data("Delhi").build()); } else { Builder builder1 = new SseEvent.Builder(); Builder builder = builder1.from(SseEvent.ofData("Goa")); builder.addClientId(str); - this.eventPublisher.publishEvent(builder.build()); + this.eventPublisher.publishEvent(SseEvent.builder().addClientId(str).event("DataOnline").data("Goa").build()); } } diff --git a/src/main/java/ch/push/notification/SseController.java b/src/main/java/ch/push/notification/SseController.java index 2723b3e..789ab6e 100644 --- a/src/main/java/ch/push/notification/SseController.java +++ b/src/main/java/ch/push/notification/SseController.java @@ -1,4 +1,4 @@ -package ch.rasc; +package ch.push.notification; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -15,15 +15,12 @@ @Controller public class SseController { - private final SseEventBus eventBus; - - @Autowired private DataEmitterService dataEmitterService; - public SseController(SseEventBus eventBus) { - this.eventBus = eventBus; - } + @Autowired + private SseEventBus eventBus; + //@GetMapping("/register/{id}") @RequestMapping(value = "/register/{id}",method = RequestMethod.GET) @@ -38,7 +35,7 @@ public SseEmitter register(@PathVariable("id") String id, @RequestHeader(value = } //this.eventBus.setDataObjectConverters(dataObjectConverters); //return this.eventBus.createSseEmitter(id, events); - return this.eventBus.createSseEmitter(id, SseEvent.DEFAULT_EVENT); + return this.eventBus.createSseEmitter(id, "DataOnline"); } diff --git a/target/classes/META-INF/maven/ch.push.notification/PUSH-NOTIFICATION/pom.xml b/target/classes/META-INF/maven/ch.push.notification/PUSH-NOTIFICATION/pom.xml new file mode 100644 index 0000000..0096365 --- /dev/null +++ b/target/classes/META-INF/maven/ch.push.notification/PUSH-NOTIFICATION/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + + ch.push.notification + PUSH-NOTIFICATION + 1.1.0 + jar + + Push Notification + Demo project for sse-eventbus library + + + + Faiz Akram + faiz.krm@gmail.com + + + + + + Apache 2 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + GitHub + https://github.com/faizakram/PUSH-NOTIFICATION.git + + + + scm:git:git@github.com:faizakram/PUSH-NOTIFICATION.git + scm:git@github.com:faizakram/PUSH-NOTIFICATION.git + https://github.com/faizakram/PUSH-NOTIFICATION.git + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.8.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + + org.springframework.boot + spring-boot-starter-undertow + + + + ch.rasc + sse-eventbus + 1.1.3 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/target/classes/static/app.js b/target/classes/static/app.js index 608a0c8..15f1a39 100644 --- a/target/classes/static/app.js +++ b/target/classes/static/app.js @@ -75,7 +75,7 @@ window.onbeforeunload = () => { } eventSource = new EventSource(`/register/${uuid}`); -eventSource.addEventListener('message', response => { +eventSource.addEventListener('DataOnline', response => { console.log(response.data); /*for (let line of response.data.split('\n')) { console.log(line);