博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HTML5 - Server-Sent Events
阅读量:7207 次
发布时间:2019-06-29

本文共 1123 字,大约阅读时间需要 3 分钟。

hot3.png

1. 服务端编码

@GetMapping(path = "/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)    public SseEmitter sse() throws Exception {        final SseEmitter emitter = new SseEmitter();        final AtomicInteger i = new AtomicInteger(0);        new Thread(new Runnable() {            @Override            public void run() {                while (true) {                    try {                        Thread.sleep(1 * 1000);                        emitter.send(i.addAndGet(1));                    } catch (InterruptedException e) {                        throw new UnsupportedOperationException("Not yet implemented!");                    } catch (IOException e) {                        throw new UnsupportedOperationException("Not yet implemented!");                    }                }            }        }).start();        return emitter;    }

2. 客户端编码

$(function () {            var source = new EventSource("/sse");            source.onmessage = function (event) {                $('#result').html(event.data);            };        });

EventSource为客户端脚本的JavaScript对象

转载于:https://my.oschina.net/DreamZhong/blog/1618560

你可能感兴趣的文章
Android基础之Java接口
查看>>
Angular开发实践(一):环境准备及框架搭建
查看>>
Vue2 源码漫游(二)
查看>>
微信浏览器下拉黑边的终极解决方案---wScroollFix
查看>>
我是如何学会爱上 Vim 的
查看>>
小tips:JS中typeof与instanceof用法
查看>>
阿里云Ecs挂载云盘
查看>>
《Kotlin项目实战开发》第1章 Kotlin是什么
查看>>
基于 react, redux 最佳实践构建的 2048
查看>>
云栖大会看技术人成长之路
查看>>
从零搭建React全家桶框架教程
查看>>
Windows command tools
查看>>
Webpack 最佳实践总结(一)
查看>>
【138天】尚学堂高淇Java300集视频精华笔记(84)
查看>>
docker与git实现push-to-deploy
查看>>
vue2.0 与 bootstrap datetimepicker的结合使用
查看>>
ant design后台模板-1.前端环境搭建
查看>>
什么是npm ?
查看>>
php 中continue break exit return 的区别
查看>>
敏捷爽畅模型及其演变——Diana Larsen专访
查看>>