2016-12-06 22 views
5

Ai đó có thể vui lòng cung cấp cho tôi ví dụ về các sự kiện phụ của máy chủ khởi động mùa xuân không?Góc 2 sự kiện máy chủ khởi động mùa xuân

Về cơ bản tôi cần phải đẩy sự kiện phía máy chủ vào trình duyệt. Tôi đang sử dụng phụ trợ 2 đầu và phần khởi động mùa xuân. Vui lòng cung cấp cho tôi 1 ví dụ mẫu, tôi không thể tìm thấy các ví dụ hay.

@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; 
    } 
} 

Làm cách nào để đẩy dữ liệu liên tục từ máy chủ và cách đăng ký sự kiện này trong Góc 2?

Cảm ơn trước

Trả lời

8

Không ai trả lời, vì vậy hãy trả lời câu hỏi của riêng tôi.

Có một bộ điều khiển Xuân Nghỉ ngơi

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); 
    } 

cuối cùng trong phần Angular2 làm điều này

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); 
     }); 
    } 
} 
+0

đây là một trợ giúp lớn, cảm ơn! – jmw5598

Các vấn đề liên quan