Loading

springmvc 中开发Server Send Event

springmvc 中开发Server Send Event

学习了:http://blog.csdn.net/leiliz/article/details/55195203

https://www.cnblogs.com/goody9807/p/4257192.html

https://www.cnblogs.com/winhu/p/3435262.html

http://blog.csdn.net/jaryle/article/details/72965885

sse的controller:

package com.stono.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

@Controller
public class SseController {
    @RequestMapping(value = "/push", produces = "text/event-stream;charset=UTF-8")
    public @ResponseBody String push(){
        Random r = new Random();
        try{
            Thread.sleep(5000);
        }catch(Exception e){
            e.printStackTrace();
        }
        return "data:Testing 1,2,3"+r.nextInt()+"\n\n";
    }
}

sse的jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page language="java" contentType="text/html; UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="msgFrompPush"></div>
    <script type="text/javascript" src="<c:out value="assets/js/jquery-1.11.1.js" />"></script>
    <script type="text/javascript">
        if(!!window.EventSource){
            var source = new EventSource("push");
            s = "";
            source.addEventListener('message', function (e) {
                console.log('---------------'+e.data);
               s+=e.data+"<br/>";
               $('#msgFrompPush').html(s);
            });

            source.addEventListener('open', function (e) {
                console.log('链接打开');
            }, false);

            source.addEventListener('error', function (e) {
                if(e.readyState == EventSource.CLOSED){
                    console.log('链接关闭');
                }else{
                    console.log(e.readyState);
                }
            }, false);
        } else {
            console.log('您的浏览器不支持SSE');
        }
    </script>
</body>
</html>

springmvc 的config

package com.stono.springmvc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import java.util.List;

@Configuration
@EnableWebMvc
@EnableScheduling
@ComponentScan("com.stono.springmvc")
public class MyMvcConfig extends WebMvcConfigurerAdapter{
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/classes/views/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("/index");
        registry.addViewController("/toUpload").setViewName("upload");
        registry.addViewController("/converter").setViewName("/converter");
        registry.addViewController("/sse").setViewName("/sse");
        registry.addViewController("/async").setViewName("/async");
    }

    @Bean
    public MultipartResolver multipartResolver(){
        CommonsMultipartResolver multipartResolver =
                new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(1000000);
        return multipartResolver;
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }

    @Bean
    public DemoInterceptor demoInterceptor(){
        return new DemoInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(demoInterceptor());
    }

    @Bean
    public MyMessageConverter converter(){
        return new MyMessageConverter();
    }
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(converter());
    }
}

 

posted @ 2018-02-12 10:44  stono  阅读(361)  评论(0编辑  收藏  举报