<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Soul Of Free Loop &#187; XMLHttpRequest</title>
	<atom:link href="https://zohead.com/archives/tag/xmlhttprequest/feed/" rel="self" type="application/rss+xml" />
	<link>https://zohead.com</link>
	<description>Uranus Zhou&#039;s Blog</description>
	<lastBuildDate>Sat, 19 Jul 2025 15:42:46 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8</generator>
	<item>
		<title>基于Chrome Socket的XMLHttpRequest</title>
		<link>https://zohead.com/archives/chrome-xhr/</link>
		<comments>https://zohead.com/archives/chrome-xhr/#comments</comments>
		<pubDate>Sat, 18 Jul 2015 16:49:35 +0000</pubDate>
		<dc:creator><![CDATA[Uranus Zhou]]></dc:creator>
				<category><![CDATA[Chrome]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[技术]]></category>
		<category><![CDATA[chrome.sockets.tcp]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[XMLHttpRequest]]></category>

		<guid isPermaLink="false">http://zohead.com/?p=999</guid>
		<description><![CDATA[之前在开发 Chrome OS 系统下的快盘文件系统时发现使用 Chrome 自带的 XMLHttpRequest 存在一些限制： 使用 jQuery 实现的 XHR 碰到 HTTP 302 redirect 等特殊的请求时浏览器会自动处理（例如：自动重定向），在 Chrome App 模式下会被限制，如果需要得到重定向的地址就不好实现，此时可以考虑用 Chrome Socket 来实现完整的 HTTP 协议请求。 有关 Chrome Socket 即 chrome.sockets.tcp 的说明可以参考这里： https://developer.chrome.com/apps/sockets [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>之前在开发 Chrome OS 系统下的快盘文件系统时发现使用 Chrome 自带的 XMLHttpRequest 存在一些限制：</p>
<p>使用 jQuery 实现的 XHR 碰到 HTTP 302 redirect 等特殊的请求时浏览器会自动处理（例如：自动重定向），在 Chrome App 模式下会被限制，如果需要得到重定向的地址就不好实现，此时可以考虑用 Chrome Socket 来实现完整的 HTTP 协议请求。</p>
<p>有关 Chrome Socket 即 chrome.sockets.tcp 的说明可以参考这里：</p>
<p><a href="https://developer.chrome.com/apps/sockets_tcp" target="_blank">https://developer.chrome.com/apps/sockets_tcp</a></p>
<p>后来发现网上已经有人实现了一个还算完整的基于 Chrome Socket 的 XMLHttpRequest：</p>
<p><a href="https://github.com/ahmadnassri/chrome.sockets.tcp.xhr" target="_blank">https://github.com/ahmadnassri/chrome.sockets.tcp.xhr</a></p>
<p>经过实际使用之后发现这个 XHR 类确实实现了基本的 HTTP 协议，但实际使用时还是有点问题，因此我在此项目基础上做了一些改进：</p>
<ul>
<li>原 chrome.sockets.tcp.xhr 不支持获取完整的 HTTP 响应内容，只能得到第一次接收到的数据，这样对于需要返回大量数据的下载等操作是不能接受的；</li>
<li>增加重定向判断，支持直接返回 HTTP 302 重定向的目标地址；</li>
<li>支持设置第一次接收到数据的超时；</li>
<li>支持所有接收的数据通过 ArrayBuffer 返回。</li>
</ul>
<p>修改过的基于 Chrome Socket 的 XMLHttpRequest 代码我放在这里了：</p>
<p><a href="https://github.com/zohead/chrome.sockets.tcp.xhr" target="_blank">https://github.com/zohead/chrome.sockets.tcp.xhr</a></p>
<p>chrome.sockets.tcp.xhr 的使用文档可以参考这里：</p>
<p><a href="http://chromesocketstcpxhr.readthedocs.org/en/latest/" target="_blank">http://chromesocketstcpxhr.readthedocs.org/en/latest/</a></p>
<p>修改之后的使用方法与这个文档里的基本一致，增加的部分例如可以通过 recvTimeout 属性指定第一次接受到数据的超时：</p>
<pre class="brush: jscript; title: ; notranslate">
var xhr = new chrome.sockets.tcp.xhr();
xhr.recvTimeout = 3500;
xhr.open('GET', 'http://google.com:80');
xhr.setRequestHeader('X-Requested-With', 'chrome.sockets.tcp.xhr');
xhr.send(null);
</pre>
<p>上面的代码就表示连接成功之后如果 3.5 秒之内没有接收到数据就认为超时断开连接。</p>
<p>由于此 chrome.sockets.tcp.xhr 项目功能实现的还不是特别完整，如果此项目使用中发现有什么问题欢迎提出并 fork 修改哦～～～</p>
]]></content:encoded>
			<wfw:commentRss>https://zohead.com/archives/chrome-xhr/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
