且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法通过移动3g网络访问服务器发送事件

更新时间:2023-11-18 23:35:52

我怀疑移动网络正在强制使用HTTP代理,该代理会在将文件转发到浏览器之前尝试缓冲文件.缓冲将使SSE消息在缓冲区中等待.

I suspect the mobile network is forcing use of an HTTP proxy that tries to buffer files before forwarding them to the browser. Buffering will make SSE messages wait in the buffer.

使用SSE,有一些技巧可以解决此类代理问题:

With SSE there are a few tricks to work around such proxies:

  • 发送消息后,关闭服务器上的连接.代理将观察文件"的末尾并转发其已缓存的所有消息.

  • Close the connection on the server after sending a message. Proxies will observe end of the "file" and forward all messages they've buffered.

这等效于长时间轮询,因此不是***选择.为避免降低所有客户端的性能,您只有在检测到必要时才可以执行此操作,例如当客户端连接时,总是发送欢迎消息.客户端应该收到该消息,如果消息没有很快到达,则通过AJAX请求将该问题报告给服务器.

This will be equivalent to long polling, so it's not optimal. To avoid reducing performance for all clients you could do it only if you detect it's necessary, e.g. when a client connects always send a welcome message. The client should expect that message and if the message doesn't arrive soon enough report the problem via an AJAX request to the server.

在消息之前或之后,在SSE注释中发送4至16KB的数据.某些代理的缓冲区大小有限,这会使缓冲区溢出,从而迫使消息输出.

Send between 4 and 16KB of data in SSE comments before or after a message. Some proxies have limited-size buffers, and this will overflow the buffer forcing messages out.

使用HTTPS.这会绕过所有第三方代理.如果可以使用HTTPS,那是***的解决方案.

Use HTTPS. This bypasses all 3rd party proxies. It's the best solution if you can use HTTPS.