SSE之fetchEventSource学习

        通过之前的SSE学习Server-Sent Events 学习-CSDN博客和SSE之eventSourceSSE之EventSource学习-CSDN博客这两篇博客的学习,我们可以发现浏览器自带的EventSource API还存在一定的限制:

  • 唯一允许传入的参数是 withCredentials;
  • 不能传入请求正文,必须在 URL 中对执行请求所需的所有信息进行编码,在大多数浏览器中限制为 2000 个字符
  • 不能传入自定义请求标头;
  • 只能发出 GET 请求 ,无法指定其他方法;
  • 如果连接被切断就无法控制重试策略,浏览器会默默地重试几次,然后就会停止,但这对于任何一种健壮的应用程序来说无疑都不够好;

        综上所述,EventSource的限制都来源它不支持POST请求,为了解决上面所陈述的那些限制,我们可以通过fetch的方式完成post相关操作。具体可以使用开源组件fetchEventSource来完成需求:GitHub - Azure/fetch-event-source: A better API for making Event Source requests, with all the features of fetch()

        首先,让我们进行安装:

npm install @microsoft/fetch-event-source

        使用效果:

import { fetchEventSource } from '@microsoft/fetch-event-source';

var that =this;
const ctrl = new AbortController()

fetchEventSource('/api/sse', {
    method: 'POST',
    headers: {
        'Content-Type': 'text/event-stream',

        'Cache-Control': 'no-cache',

        'Connection': 'keep-alive'
    },
    body: JSON.stringify({
         foo: 'bar'
    }),
    signal: ctrl.signal,

    async onopen(response) {//建立连接的回调
        if (response.ok && response.headers.get('content-type') === EventStreamContentType) {
            return; // everything's good
        } else if (response.status >= 400 && response.status < 500 && response.status !== 429) {
            // client-side errors are usually non-retriable:
            throw new FatalError();
        } else {
            throw new RetriableError();
        }
    },

    onmessage(msg) { //接收一次数据段时回调,因为是流式返回,所以这个回调会被调用多次
        // if the server emits an error message, throw an exception
        // so it gets handled by the onerror callback below:
        if (msg.event === 'FatalError') {
            throw new FatalError(msg.data);
        }
    },

    onclose() {//正常结束的回调
        that.controller.abort()  //关闭连接
    },

    onerror(err) {//连接出现异常回调
        // 必须抛出错误才会停止
        if (err instanceof FatalError) {
            throw err; // rethrow to stop the operation
        } else {
            // do nothing to automatically retry. You can also
            // return a specific retry interval here.
        }
    }

});

参考与推荐:

@microsoft/fetch-event-source - npm (npmjs.com)

chatgpt 逐字输出 使用fetch/eventSource/fetchEventSouce进行sse流式处理-CSDN博客

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSE4 Home › Articles by Kiefer Kuah April 2007 Intel Software Solutions Group Abstract Intel® SSE4 is a new set of Single Instruction Multiple Data (SIMD) instructions that will be introduced in the 45nm Next Generation Intel® Core™2 processor family (Penryn) and improve the performance and energy efficiency of a broad range of applications. This white paper describes how video encoders can utilize Intel SSE4 instructions to achieve 1.6x to 3.8x performance speedups in integer motion vector search, a frequently used motion estimation function. Contents 1. Introduction 2. Motion Estimation Using MPSADBW and PHMINPOSUW 3. Results 4. Conclusion A. SSE2 - Optimized Function for 4x4 Blocks B. Intel® SSE4 - Optimized Function for 4x4 Blocks C. SSE2 - Optimized Function for 8x8 Blocks D. Intel® SSE4 - Optimized Function for 8x8 Blocks E. SSE2 - Optimized Function for 16x16 Blocks F. Intel® SSE4 - Optimized Function for 16x16 Blocks 1. Introduction Intel® Streaming SIMD Extensions 4 (Intel® SSE4) is a new set of Single Instruction Multiple Data (SIMD) instructions designed to improve the performance of various applications, such video encoders, image processing, and 3D games. Intel SSE4 builds upon the Intel® 64 and IA-32 instruction set, the most popular and broadly used computer architecture for developing 32-bit and 64-bit applications. Intel SSE4 will be introduced in the 45nm Next Generation Intel® Core™2 processor family (Penryn). This white paper will describe how video encoders can benefit from the Intel SSE4 instructions, achieving 1.6x to 3.8x performance speedups in integer motion vector search, a frequently used motion estimation function. Three different block sizes, 4x4, 8x8, and 16x16, are used in this paper to represent some of the variations that are used in motion estimation and to illustrate how the code can be adapted to suit these variations. 2. Motion Estimation Using MPSADBW and PHMINPOSUW Motion estimation is one of the main bottlenecks in video encoders. It involves searching reference frames for best matches and often accounts for about 40% of the total CPU cycles consumed by an encoder. The quality of the search is a factor that determines the compression ratio and the video quality of the enco ded video. This search operation is often the target of algorithmic and SIMD optimizations to improve the encoding speed. An un-optimized version of the block matching function for 4x4 block size is shown in Figure 2 -1. The example code in this paper performs only the integer motion vector search of the motion estimation stage. -F cigoullarpes 2e- s1o. uUrcneovpietwim pilzaeindc oVpeyr tsoi oclnip boof aarndp Irnintte?ger Block Matching Function

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值