028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

SpringMVC4.1服务器端推送的示例分析

这篇文章主要介绍了Spring MVC4.1服务器端推送的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

创新互联公司-专业网站定制、快速模板网站建设、高性价比周宁网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式周宁网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖周宁地区。费用合理售后完善,10多年实体公司更值得信赖。

服务器端推送

示例

服务器推送控制器

package com.wisely.web;

import java.util.Random;

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

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

页面代码

"> if (!!window.EventSource) {     var source = new EventSource('push'); //为http://localhost:8080/testSpringMVC/push     s='';     source.addEventListener('message', function(e) {       s+=e.data+"
"       $("#msg_from_server").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");   }

效果

Spring MVC4.1服务器端推送的示例分析

感谢你能够认真阅读完这篇文章,希望小编分享的“Spring MVC4.1服务器端推送的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


本文标题:SpringMVC4.1服务器端推送的示例分析
分享URL:http://www.tsicrk.com/article/ggoced.html

其他资讯

让你的专属顾问为你服务

2.2517s