EventSource使用哪种HTTP方法打开连接? [英] What HTTP Method does EventSource use to open a connection?

查看:136
本文介绍了EventSource使用哪种HTTP方法打开连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然人们在其他问题上声称EventSource有充分的文献记载,但我发现它在某些情况下比实际情况隐含得多.

While in other questions people claimt EventSource is fairly well documented I have found it to be more implied then explicit in some cases.

我的理解是,当您在JS中初始化EventSource对象时,它将使用指定的URI打开与服务器的连接.

My understanding is that when you initialise an EventSource object in JS it opens a connection to your server using the specified URI.

此连接是否使用GET发起?

Is this connection initiated using GET?

(不确定这是否构成第二个问题)是否可以使用/强制使用其他HTTP方法(POST)?

(Not sure if this constitutes a second question) Is it possible to use/force another HTTP Method (POST)?

推荐答案

使用EventSource接口时,请求方法是GET请求.您可以在传递给构造函数的URL中包含查询字符串,然后在服务器上解析查询字符串.

The request method when using the EventSource interface is a GET request. You can include a query string in the URL passed to the constructor and parse the query string at the server.

const stream = "data: event stream\n\n";
const blob = new Blob([stream], {type:"text/event-stream"});
const blobURL = URL.createObjectURL(blob);
const es = new EventSource(blobURL);
es.onmessage = e => {
   console.log(e.data);
}
es.onerror = e => {
   es.close();
}

这篇关于EventSource使用哪种HTTP方法打开连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆