What HTTP Method does EventSource use to open a connection?

前提是你 提交于 2019-12-13 03:57:19

问题


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

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

Is this connection initiated using GET?

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


回答1:


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();
}


来源:https://stackoverflow.com/questions/48372650/what-http-method-does-eventsource-use-to-open-a-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!