有人可以为我提供弹簧启动服务器端事件的示例吗?
基本上我需要将服务器端事件推送到浏览器。我使用角度2和弹簧启动后端。 请给我一个示例,我找不到好的例子。
@Controller
public class SSEController {
private final List<SseEmitter> emitters = new ArrayList<>();
@RequestMapping(path = "/stream", method = RequestMethod.GET)
public SseEmitter stream() throws IOException {
SseEmitter emitter = new SseEmitter();
emitters.add(emitter);
emitter.onCompletion(() -> emitters.remove(emitter));
return emitter;
}
}
如何从服务器连续推送数据以及如何在Angular 2中订阅此事件?
提前致谢
答案 0 :(得分:19)
没有人回答,所以回答我自己的问题。
有一个Spring Rest控制器
<强> SseController.java 强>
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@RestController
public class SSEController {
public static final List<SseEmitter> emitters = Collections.synchronizedList( new ArrayList<>());
@RequestMapping(path = "/stream", method = RequestMethod.GET)
public SseEmitter stream() throws IOException {
SseEmitter emitter = new SseEmitter();
emitters.add(emitter);
emitter.onCompletion(() -> emitters.remove(emitter));
return emitter;
}
}
ServiceClass.java
public void sendSseEventsToUI(Notification notification) { //your model class
List<SseEmitter> sseEmitterListToRemove = new ArrayList<>();
SSEController.emitters.forEach((SseEmitter emitter) -> {
try {
emitter.send(notification, MediaType.APPLICATION_JSON);
} catch (IOException e) {
emitter.complete();
sseEmitterListToRemove.add(emitter);
e.printStackTrace();
}
});
SSEController.emitters.removeAll(sseEmitterListToRemove);
}
最后在Angular2组件中执行此操作
<强> notification.component.ts 强>
import {Component, OnInit} from '@angular/core';
declare let EventSource:any;
@Component({
selector: 'notification-cmp',
templateUrl: 'notification.component.html'
})
export class NotificationComponent implements OnInit {
connect(): void {
let source = new EventSource('http://localhost:8080/stream');
source.addEventListener('message', message => {
let n: Notification; //need to have this Notification model class in angular2
n = JSON.parse(message.data);
console.log(message.data);
});
}
}
答案 1 :(得分:1)
Pratap A.K的回答很棒。但是,为了使它更简洁一点,您应该创建一个实现接口的NotificationService。像这样:
NotificationServiceImpl.java
public class NotificationServiceImpl implements NotificationService {
public static final List<SseEmitter> emitters = Collections.synchronizedList(new ArrayList<>());
@Override
public SseEmitter initSseEmitters() {
SseEmitter emitter = new SseEmitter();
emitters.add(emitter);
emitter.onCompletion(() -> emitters.remove(emitter));
return emitter;
}
@Override
public void sendSseEventsToUI(WebSource notification) {
List<SseEmitter> sseEmitterListToRemove = new ArrayList<>();
this.emitters.forEach((SseEmitter emitter) -> {
try {
emitter.send(notification, MediaType.APPLICATION_JSON);
} catch (IOException e) {
emitter.complete();
sseEmitterListToRemove.add(emitter);
e.printStackTrace();
}
});
this.emitters.removeAll(sseEmitterListToRemove);
}
}
NotificationService.java
public interface NotificationService {
public SseEmitter initSseEmitters();
public void sendSseEventsToUI(WebSource notification);
}
SSEController.java
@RestController
@RequestMapping("/mystream")
public class SSEController {
@Autowired
NotificationServiceImpl INotificationServiceImpl;
@CrossOrigin
@RequestMapping(path = "/streamsource", method = RequestMethod.GET)
public SseEmitter stream() throws IOException {
return INotificationServiceImpl.initSseEmitters();
}
}
最诚挚的问候
答案 2 :(得分:0)
上面的答案很有帮助。
还有..
接收推送的实际数据。
代码应为
[[ 1. 2.]
[ 3. 4.]
[ 5. 6.]
[ 7. 8.]
[ 9. 10.]]
10
5
答案 3 :(得分:0)
现在使用Spring Webflux可以更轻松地完成该任务,只需使用MediaTypes即可:
@GetMapping(value = "/queue/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<EventNotification> streamEvents() {
return managerService.streamEvents();
}
您也可以使用RSocket示例在https://github.com/htenjo/vqueue中检查有效的实现。