使用SSE从springboot发送通知到ionic

时间:2019-07-16 09:45:49

标签: angular spring-boot ionic-framework

我想用sse从sprint发送通知到ionic,但是尝试时我什么也没收到。 我没有在角度部分得到结果,也没有出错。 任何人都可以帮助我!

MyDataController.java

package com.example.SseTest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;

@RestController
public class MyDataController {
    @Autowired
    private
    StatusInProgress statusInProgress;

    @CrossOrigin(exposedHeaders = "Access-Control-Allow-Origin")
    @RequestMapping(value = "/get_mydata")
    public SseEmitter getMyData() throws InterruptedException {
        SseEmitter notifier = new SseEmitter(600L);
        try {
            String myData = statusInProgress.getMyData();
            if (myData != null && myData != "") {
                notifier.send(SseEmitter.event().reconnectTime(500).data(myData));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return notifier;
    }
}

StatusInProgress.java

package com.example.SseTest;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
@Service
public class StatusInProgress {
    private static Map<String, String> storeData = new HashMap<String, String>();


public void triggerEvent(Notification eventData) {
    System.out.println("Display To Angular App");
    try {System.out.println(eventData.getData());
        ObjectMapper mapper = new ObjectMapper();
        String jsonText = mapper.writeValueAsString(eventData);
        putMyData(jsonText);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}@CachePut(value = "myData")
public void putMyData(String myData) {
    if (storeData.containsKey("mydata")) {
        storeData.remove("mydata");
    }storeData.put("mydata", myData);
}@Cacheable(value = "myData")
public String getMyData() {
    String myitem= "";
    try {
        myitem = storeData.get("mydata");
    } catch (Exception e) {
    }return myitem;
}
}

app.component.ts

import { Component, ViewChild } from "@angular/core";
import { OnInit } from '@angular/core';
import { Platform, Nav } from "ionic-angular";
import {Observable} from "rxjs";
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Keyboard } from '@ionic-native/keyboard';

import { HomePage } from "../pages/home/home";
import { LoginPage } from "../pages/login/login";
import { Notification } from "./notification";

export interface MenuItem {
    title: string;
    component: any;
    icon: string;
}

@Component({
  templateUrl: 'app.html'
})

export class MyApp {
EventSource:any;
  @ViewChild(Nav) nav: Nav;

  rootPage: any = LoginPage;
  myData: any;

  appMenuItems: Array<MenuItem>;

  constructor(
    public platform: Platform,
    public statusBar: StatusBar,
    public splashScreen: SplashScreen,
    public keyboard: Keyboard  ) {
    this.initializeApp();
    this.connect();

    this.appMenuItems = [
      {title: 'Home', component: HomePage, icon: 'home'},
    ];
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.statusBar.overlaysWebView(false);
    });
  }

  openPage(page) {
    this.nav.setRoot(page.component);
  }

  logout() {
    this.nav.setRoot(LoginPage);
  }
  connect(): void {
    let source = new EventSource('http://localhost:8080/get_mydata'); 
    source.addEventListener('message', message => {
        this.myData = JSON.parse(message.data); 
        console.log(this.myData);       
    });
 }

}

我创建一个包含要发送消息的通知类。

0 个答案:

没有答案