最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Listen to http request on webpage - Stack Overflow

programmeradmin1浏览0评论

I'm now creating a script on my website, which will need a bi-direction connection (the script is a chat room window). But we don't want to create a socket. Instead, we want to make client(the script) and server both have ability of sending http request to each other.
(BTW, the website server and script server are two different servers)

It is very easy for client(the script) to send http request to our server. But it is a big problem for the client(the script) to listen to http requests.

I have done some search but found nothing, maybe this requirement is so weird that seldom been used? Is it possible for a script which embeded in webpage that listen for http request?

Thanks!

I'm now creating a script on my website, which will need a bi-direction connection (the script is a chat room window). But we don't want to create a socket. Instead, we want to make client(the script) and server both have ability of sending http request to each other.
(BTW, the website server and script server are two different servers)

It is very easy for client(the script) to send http request to our server. But it is a big problem for the client(the script) to listen to http requests.

I have done some search but found nothing, maybe this requirement is so weird that seldom been used? Is it possible for a script which embeded in webpage that listen for http request?

Thanks!

Share Improve this question asked Aug 15, 2016 at 13:21 Hubert LinHubert Lin 973 silver badges8 bronze badges 6
  • Use WebSockets and keep a persistant connection open. – Phylogenesis Commented Aug 15, 2016 at 13:24
  • 1 if you dont want socket, try this html5rocks./en/tutorials/eventsource/basics – Ja9ad335h Commented Aug 15, 2016 at 13:30
  • Great! That's almost what I am finding! But seems like IE/edge does not support that!? – Hubert Lin Commented Aug 15, 2016 at 13:39
  • What's the reason you don't want to create a socket? – deceze Commented Aug 15, 2016 at 13:44
  • That's a bit plicated. If I can, I would choose to use socket create connection. But I need a back up plan if my teammate cannot create a server which capable of creating socket. – Hubert Lin Commented Aug 15, 2016 at 14:25
 |  Show 1 more ment

1 Answer 1

Reset to default 4

In order to receive an HTTP request, you must:

  1. have a publicly accessible IP address
  2. have an open port, publicly accessible
  3. bind a program to that port to listen for HTTP requests

Browsers fall down on all three counts. You cannot expect that all of your clients have a publicly reachable, unblocked IP address with specifically the port open that you want. But even if that were the case, there's no way for the browser to listen to ining requests; there's no API to do that in the browser, partly because the browser is an HTTP client and not a server, partly because offering such an API would probably provide an extremely powerful API to all sorts of attackers, and partly because it oftentimes is useless anyway because the browser cannot be reached anyway (see point 1).

So, no, you cannot turn the browser into an HTTP server.

Use WebSockets.

发布评论

评论列表(0)

  1. 暂无评论