Skip to content

Commit

Permalink
commited
Browse files Browse the repository at this point in the history
  • Loading branch information
faizakram committed Nov 9, 2017
1 parent 50092a8 commit c17ffef
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 20 deletions.
20 changes: 9 additions & 11 deletions src/main/java/ch/push/notification/DataEmitterService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String> list = new ArrayList<String>();

//private final static Random random = new Random();

public DataEmitterService(ApplicationEventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}
@Autowired
private ApplicationEventPublisher eventPublisher;

public void addListInfo(String id)
{
Expand All @@ -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());
}
}

Expand Down
13 changes: 5 additions & 8 deletions src/main/java/ch/push/notification/SseController.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.rasc;
package ch.push.notification;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
Expand All @@ -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)
Expand All @@ -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");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ch.push.notification</groupId>
<artifactId>PUSH-NOTIFICATION</artifactId>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>Push Notification</name>
<description>Demo project for sse-eventbus library</description>

<developers>
<developer>
<name>Faiz Akram</name>
<email>[email protected]</email>
</developer>
</developers>

<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<issueManagement>
<system>GitHub</system>
<url>https://github.com/faizakram/PUSH-NOTIFICATION.git</url>
</issueManagement>

<scm>
<connection>scm:git:[email protected]:faizakram/PUSH-NOTIFICATION.git</connection>
<developerConnection>scm:[email protected]:faizakram/PUSH-NOTIFICATION.git</developerConnection>
<url>https://github.com/faizakram/PUSH-NOTIFICATION.git</url>
</scm>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath />
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

<dependency>
<groupId>ch.rasc</groupId>
<artifactId>sse-eventbus</artifactId>
<version>1.1.3</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
2 changes: 1 addition & 1 deletion target/classes/static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c17ffef

Please sign in to comment.