<?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>Sann-Remy Chea&#039;s blog</title>
	<atom:link href="http://srchea.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://srchea.com/blog</link>
	<description>An Enthusiastic Developer</description>
	<lastBuildDate>Sun, 19 May 2013 20:33:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Experimenting with Web Audio API + Three.js (WebGL)</title>
		<link>http://srchea.com/blog/2013/05/experimenting-with-web-audio-api-three-js-webgl/</link>
		<comments>http://srchea.com/blog/2013/05/experimenting-with-web-audio-api-three-js-webgl/#comments</comments>
		<pubDate>Thu, 09 May 2013 10:16:38 +0000</pubDate>
		<dc:creator>Sann-Remy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Web Audio API]]></category>
		<category><![CDATA[WebGL]]></category>

		<guid isPermaLink="false">http://srchea.com/blog/?p=236</guid>
		<description><![CDATA[Here is a post about my experiment of Web Audio API. Throughout this article, I will show how I made the sound visualizer in WebGL. See the live demo (works with the latest Google Chrome and Firefox Nightly) Now, let&#8217;s see how we load the music and make the cube moving. As usual, you can [...]]]></description>
				<content:encoded><![CDATA[<p>Here is a post about my experiment of Web Audio API. Throughout this article, I will show how I made the sound visualizer in WebGL.</p>
<p class="center"><a style="margin-right: 8px;" href="http://srchea.com/blog/wp-content/uploads/2013/05/sound-visualizer-web-audio-api-webgl-1.jpg"><img class="alignnone size-medium wp-image-238" title="Sound Visualizer (Web Audio API and WebGL)" alt="Sound Visualizer (Web Audio API and WebGL)" src="http://srchea.com/blog/wp-content/uploads/2013/05/sound-visualizer-web-audio-api-webgl-1-300x161.jpg" width="300" height="161" /></a><a style="margin-left: 8px;" href="http://srchea.com/blog/wp-content/uploads/2013/05/sound-visualizer-web-audio-api-webgl-2.jpg"><img class="alignnone size-medium wp-image-240" title="Sound Visualizer (Web Audio API and WebGL)" alt="Sound Visualizer (Web Audio API and WebGL)" src="http://srchea.com/blog/wp-content/uploads/2013/05/sound-visualizer-web-audio-api-webgl-2-300x161.jpg" width="300" height="161" /></a><br />
<strong><a href="http://srchea.com/apps/sound-visualizer-web-audio-api-webgl/">See the live demo (works with the latest Google Chrome and Firefox Nightly)</a></strong></p>
<p>Now, let&#8217;s see how we load the music and make the cube moving. As usual, you can find <a href="https://github.com/srchea/Sound-Visualizer/" target="_blank">all the source code on my github account</a>.</p>
<ol>
<li><a href="http://srchea.com/blog/2013/05/experimenting-with-web-audio-api-three-js-webgl/#p1">Web Audio API part: Load the music and get the frequency data</a></li>
<li><a href="http://srchea.com/blog/2013/05/experimenting-with-web-audio-api-three-js-webgl/#p2">WebGL part: Make the cubes react regarding the sound</a></li>
<li><a href="http://srchea.com/blog/2013/05/experimenting-with-web-audio-api-three-js-webgl/#p3">Useful links and free resources</a></li>
</ol>
<p><span id="more-236"></span></p>
<h1>Web Audio API part: Load the music and get the frequency data<a name="p1"></a></h1>
<p>The main object of the Web Audio API is the AudioContext object. At the time that I&#8217;m writing that post, only Google Chrome and Firefox Nightly are supporting the AudioContext. Note that the class name is prefixed by webkit for Chrome.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> webkitAudioContext <span style="color: #339933;">===</span> <span style="color: #3366CC;">'function'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// webkit-based</span>
        context <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> webkitAudioContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// other browsers that support AudioContext</span>
        context <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> AudioContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Web Audio API is not supported in this browser</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To load the file, we need to use a response type specified in the <a href="https://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html" target="_blank">XMLHttpRequest level 2</a>: <a href="http://www.khronos.org/registry/typedarray/specs/latest/#ARRAYBUFFER" target="_blank">ArrayBuffer</a>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
request.<span style="color: #660066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
request.<span style="color: #660066;">responseType</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;arraybuffer&quot;</span><span style="color: #339933;">;</span>
&nbsp;
request.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// decode audio data</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The W3C advises us on using this method instead of createBuffer method because &#8220;it is asynchronous and does not block the main JavaScript thread&#8221;. After that, we need to decode the array buffer with the decodeAudioData method.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">AudioContext.<span style="color: #660066;">decodeAudioData</span><span style="color: #009900;">&#40;</span>audioData<span style="color: #339933;">,</span> successCallback<span style="color: #339933;">,</span> errorCallback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Then we need to create AudioNode objects for various things:</p>
<ul>
<li>ScriptProcessorNode, created with the AudioContext.createJavaScriptNode(bufferSize), it handles on how many the onaudioprocess event is dispatched. The bufferSize must take these values: 256, 512, 1024, 2048, 4096, 8192, 16384.</li>
<li>AnalyserNode, created with AudioContext.createAnalyser(), it provides real-time frequency and time-domain analysis information.</li>
<li>AudioBufferSourceNode, created with AudioContext.createBufferSource(), that is for the playback.</li>
</ul>
<p>After creating these nodes, we need to connect them. The node source is connected to the analyser node which is connected to the script processor node. The main node is connected to the destination, that is to say the sound card.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">AudioBufferSourceNode.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>AnalyserNode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
AnalyserNode.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>ScriptProcessorNode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
AudioBufferSourceNode.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>AudioContext.<span style="color: #660066;">destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p>Now, we are getting the binaries from frequencies at the current time and copy the data into unsigned byte array. We will use the array to scale the cubes regarding the values.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">ScriptProcessorNode.<span style="color: #660066;">onaudioprocess</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    array <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Uint8Array<span style="color: #009900;">&#40;</span>AnalyserNode.<span style="color: #660066;">frequencyBinCount</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    AnalyserNode.<span style="color: #660066;">getByteFrequencyData</span><span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><b>Important note:</b> if the audio process event stops during the playback, it is because the event is defined in a function. To fix it, you need to bind to the window object (make it global). Actually, the garbage collector is called and it cleaned the variable. Here is <a href="http://lists.w3.org/Archives/Public/public-audio/2013JanMar/0304.html" target="_blank">the thread about that issue</a>.</p>
<p>To start playing the music, we are using the noteOn() method.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">AudioBufferSourceNode.<span style="color: #660066;">noteOn</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Here is the full source code explained above:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> context<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> source<span style="color: #339933;">,</span> sourceJs<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> analyser<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> buffer<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">'my_music.ogg'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> array <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> <span style="">Array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
request.<span style="color: #660066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
request.<span style="color: #660066;">responseType</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;arraybuffer&quot;</span><span style="color: #339933;">;</span>
&nbsp;
request.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    context.<span style="color: #660066;">decodeAudioData</span><span style="color: #009900;">&#40;</span>
        request.<span style="color: #660066;">response</span><span style="color: #339933;">,</span>
        <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>buffer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// Error decoding file data</span>
                <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            sourceJs <span style="color: #339933;">=</span> context.<span style="color: #660066;">createJavaScriptNode</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2048</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            sourceJs.<span style="color: #660066;">buffer</span> <span style="color: #339933;">=</span> buffer<span style="color: #339933;">;</span>
            sourceJs.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>context.<span style="color: #660066;">destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            analyser <span style="color: #339933;">=</span> context.<span style="color: #660066;">createAnalyser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            analyser.<span style="color: #660066;">smoothingTimeConstant</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.6</span><span style="color: #339933;">;</span>
            analyser.<span style="color: #660066;">fftSize</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">512</span><span style="color: #339933;">;</span>
&nbsp;
            source <span style="color: #339933;">=</span> context.<span style="color: #660066;">createBufferSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            source.<span style="color: #660066;">buffer</span> <span style="color: #339933;">=</span> buffer<span style="color: #339933;">;</span>
&nbsp;
            source.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>analyser<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            analyser.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>sourceJs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            source.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>context.<span style="color: #660066;">destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            sourceJs.<span style="color: #660066;">onaudioprocess</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                array <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Uint8Array<span style="color: #009900;">&#40;</span>analyser.<span style="color: #660066;">frequencyBinCount</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                analyser.<span style="color: #660066;">getByteFrequencyData</span><span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            source.<span style="color: #660066;">noteOn</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// Decoding error</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h1>WebGL part: Make the cubes react regarding the sound<a name="p2"></a></h1>
<p>First of all, we create the scene and the cubes into it.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> scene <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Scene</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> cubes <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> <span style="">Array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">;</span> x <span style="color: #339933;">+=</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    cubes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> <span style="">Array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">;</span> y <span style="color: #339933;">+=</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> geometry <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">CubeGeometry</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1.5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">var</span> material <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">MeshPhongMaterial</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            color<span style="color: #339933;">:</span> randomFairColor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            ambient<span style="color: #339933;">:</span> 0x808080<span style="color: #339933;">,</span>
            specular<span style="color: #339933;">:</span> 0xffffff<span style="color: #339933;">,</span>
            shininess<span style="color: #339933;">:</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span>
            reflectivity<span style="color: #339933;">:</span> <span style="color: #CC0000;">5.5</span> 
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        cubes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Mesh</span><span style="color: #009900;">&#40;</span>geometry<span style="color: #339933;">,</span> material<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cubes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">position</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        scene.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>cubes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        j<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    i<span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Then, we are going to rescale 225 cubes on the z axis. This code goes in a function called by requestAnimationFrame(). Note that array is build from the audioprocess event and contains the bytes of frequencies.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> k <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> cubes.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> cubes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> scale <span style="color: #339933;">=</span> array<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">;</span>
        cubes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">scale</span>.<span style="color: #660066;">z</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>scale <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">:</span> scale<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        k <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">&lt;</span> array.<span style="color: #660066;">length</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You can find the full source code <a href="https://github.com/srchea/Sound-Visualizer/blob/master/js/visualizer.js" target="_blank">here</a> and the whole applications <a href="https://github.com/srchea/Sound-Visualizer/" target="_blank">here</a>.</p>
<h1>Useful links and free resources<a name="p3"></a></h1>
<ul>
<li><a href="https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html" target="_blank">W3C&#8217;s Web Audio API specifications</a></li>
<li><a href="http://www.smartjava.org/content/exploring-html5-web-audio-visualizing-sound" target="_blank">Exploring the HTML5 Web Audio: visualizing sound</a></li>
<li><a href="http://www.html5rocks.com/en/tutorials/webaudio/intro/" target="_blank">Getting Started with Web Audio API</a></li>
<li><a href="http://www.looperman.com/tracks" target="_blank">Looperman (free music resources)</a></li>
<li><a href="https://github.com/srchea/Sound-Visualizer" target="_blank">Source code from GitHub</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://srchea.com/blog/2013/05/experimenting-with-web-audio-api-three-js-webgl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making-of: Pure CSS3 logos</title>
		<link>http://srchea.com/blog/2012/11/making-of-pure-css3-logos/</link>
		<comments>http://srchea.com/blog/2012/11/making-of-pure-css3-logos/#comments</comments>
		<pubDate>Sun, 18 Nov 2012 21:02:29 +0000</pubDate>
		<dc:creator>Sann-Remy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTML5/CSS3]]></category>

		<guid isPermaLink="false">http://srchea.com/blog/?p=147</guid>
		<description><![CDATA[You guys have probably seen some awesome logos designed in pure CSS3 around the Web such as Opera, Internet Exporer, Twitter and many more. I wanted to try to make ones, so I decided to make logos of my working company (this totally was a personal choice and a personal work). After a couple of [...]]]></description>
				<content:encoded><![CDATA[<p>You guys have probably seen some awesome logos designed in pure CSS3 around the Web such as <a title="Opera logo in CSS" href="http://v2.desandro.com/articles/opera-logo-css/" target="_blank">Opera</a>, <a title="IE logo in CSS" href="http://cordobo.com/wp-content/uploads/ie-pure-css-logo/" target="_blank">Internet Exporer</a>, <a title="Twitter logo in CSS" href="http://labs.upperdog.se/twitter-logo-in-css/" target="_blank">Twitter</a> and <a title="More CSS logos" href="http://www.1stwebdesigner.com/css/css-icons-logos/" target="_blank">many more</a>. I wanted to try to make ones, so I decided to make logos of my working company (this totally was a personal choice and a personal work).<br />
After a couple of hours of work, here is the result:</p>
<p class="center"><a href="http://srchea.com/apps/css3-ubisoft-owlient-logos/"><img class="size-full wp-image-148 aligncenter" title="Pure CSS3 logos of Ubisoft/Owlient" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo.png" alt="Pure CSS3 logos of Ubisoft/Owlient" width="600" height="209" /><br />
<strong>Click here to see live demo</strong></a></p>
<p>Of course, for better results you need to use latest browser versions which support radial gradient backgrounds, transformations, border radius, etc. If you are using the latest Chrome, Safari, Firefox, Opera or Internet Explorer 10, that will be fine.</p>
<ol>
<li><a href="http://srchea.com/blog/2012/11/making-of-pure-css3-logos/#p1">The first approach</a></li>
<li><a href="http://srchea.com/blog/2012/11/making-of-pure-css3-logos/#p2">Browser rendering results</a>
<ol>
<li><a href="http://srchea.com/blog/2012/11/making-of-pure-css3-logos/#p2-1">Google Chrome (10, 11, 14, 23)</a></li>
<li><a href="http://srchea.com/blog/2012/11/making-of-pure-css3-logos/#p2-2">Mozilla Firefox (3.6, 4, 5, 16)</a></li>
<li><a href="http://srchea.com/blog/2012/11/making-of-pure-css3-logos/#p2-3">Opera (10.50, 11.11, 11.64, 12.0)</a></li>
<li><a href="http://srchea.com/blog/2012/11/making-of-pure-css3-logos/#p2-4">Internet Explorer (7, 8, 9, 10 preview)</a></li>
</ol>
</li>
<li><a href="http://srchea.com/blog/2012/11/making-of-pure-css3-logos/#p3">Debugging/Helping tools</a></li>
</ol>
<p><span id="more-147"></span></p>
<h1>The first approach<a name="p1"></a></h1>
<p>First of all, I needed to figure out how to build the structure of the logo with HTML elements only: I had to decompose the picture in different layers (I mean the <code>&lt;div&gt;</code>) and to think about the way to design them with only CSS properties in the same time. In other words, there are many conditions and constraints to be aware of.</p>
<p>For the structure, the first Ubisoft logo contains 16 layers with 10 full circles (radial gradients and shadows) and 6 for details (colorized borders). The green one has 10 full circles and the Owlient logo has only 4, ears are in a <code>&lt;div&gt;</code> actually.</p>
<h1>Browser rendering results</h1>
<p>Here are some screenshots from different browser versions. I deliberately chose browsers regarding their rendering engines to have a larger panel for this showcase: WebKit (Chrome, Safari), Gecko (Firefox), Presto (Opera) and Trident-based (Internet Explorer).</p>
<h2>Google Chrome<a name="p2-1"></a></h2>
<p>Chrome actually supports background gradients, but the keyword radial has to come inside the <code>-webkit-gradient(radial, [...])</code> instead of <code>-webkit-radial-gradient([...])</code>. This attribute came in the version 11.</p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-chrome10.png"><img class="aligncenter size-medium wp-image-165" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Google Chrome 10)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-chrome10-300x82.png" alt="Pure CSS3 logos (Google Chrome 10)" width="300" height="82" />version 10</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-chrome11.png"><img class="aligncenter size-medium wp-image-166" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Google Chrome 11)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-chrome11-300x80.png" alt="Pure CSS3 logos (Google Chrome 11)" width="300" height="80" />version 11</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-chrome14.png"><img class="aligncenter size-medium wp-image-167" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Google Chrome 14)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-chrome14-300x80.png" alt="Pure CSS3 logos (Google Chrome 14)" width="300" height="80" />version 14</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-chrome23.png"><img class="aligncenter size-medium wp-image-168" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Google Chrome 23)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-chrome23-300x80.png" alt="Pure CSS3 logos (Google Chrome 23)" width="300" height="80" />version 23</a></p>
<h2>Mozilla Firefox<a name="p2-2"></a></h2>
<p>There is no prefix on the <code>border-radius</code> properties, that is why Firefox 3.6 doesn&#8217;t curve the blocks, but supports <code>-moz-border-radius</code>. Other properties/values have <code>-moz</code> prefix, such as <code>transform</code> (rotation) and <code>radial-gradient</code>.</p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ff3-6.png"><img class="aligncenter size-medium wp-image-169" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Mozilla Firefox 3.6)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ff3-6-300x79.png" alt="Pure CSS3 logos (Mozilla Firefox 3.6)" width="300" height="79" />version 3.6</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ff4.png"><img class="aligncenter size-medium wp-image-170" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Mozilla Firefox 4)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ff4-300x80.png" alt="Pure CSS3 logos (Mozilla Firefox 4)" width="300" height="80" />version 4</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ff5.png"><img class="aligncenter size-medium wp-image-171" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Mozilla Firefox 5)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ff5-300x80.png" alt="Pure CSS3 logos (Mozilla Firefox 5)" width="300" height="80" />version 5</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ff16.png"><img class="aligncenter size-medium wp-image-172" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Mozilla Firefox 16)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ff16-300x80.png" alt="Pure CSS3 logos (Mozilla Firefox 16)" width="300" height="80" />version 16</a></p>
<h2>Opera<a name="p2-3"></a></h2>
<p>It seems that the version 11.11 doesn&#8217;t support the <code>radial-gradient</code> position (<code>farthest-corner</code>).</p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-opera10-50.png"><img class="aligncenter size-medium wp-image-177" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Opera 10.50)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-opera10-50-300x82.png" alt="Pure CSS3 logos (Opera 10.50)" width="300" height="82" />version 10.50</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-opera11-11.png"><img class="aligncenter size-medium wp-image-178" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Opera 11.11)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-opera11-11-300x80.png" alt="Pure CSS3 logos (Opera 11.11)" width="300" height="80" />version 11.11</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-opera11-64.png"><img class="aligncenter size-medium wp-image-179" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Opera 11.64)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-opera11-64-300x80.png" alt="Pure CSS3 logos (Opera 11.64)" width="300" height="80" />version 11.64</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-opera12.png"><img class="aligncenter size-medium wp-image-180" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Opera 12.0)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-opera12-300x80.png" alt="Pure CSS3 logos (Opera 12.0)" width="300" height="80" />version 12.0</a></p>
<h2>Internet Explorer<a name="p2-4"></a></h2>
<p>No CSS3 support in IE7 and 8 but IE9 supports <code>border-radius</code> and <code>box-shadow</code> properties.</p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ie7.png"><img class="aligncenter size-medium wp-image-198" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Internet Explorer 7)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ie7-300x88.png" alt="Pure CSS3 logos (Internet Explorer 7)" width="300" height="88" />version 7</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ie8.png"><img class="aligncenter size-medium wp-image-174" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Internet Explorer 8)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ie8-300x93.png" alt="Pure CSS3 logos (Internet Explorer 8)" width="300" height="93" />version 8</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ie9.png"><img class="aligncenter size-medium wp-image-175" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Internet Explorer 9)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ie9-300x83.png" alt="Pure CSS3 logos (Internet Explorer 9)" width="300" height="83" />version 9</a></p>
<p class="center"><a href="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ie10.png"><img class="aligncenter size-medium wp-image-176" style="vertical-align: middle; margin-right: 20px;" title="Pure CSS3 logos (Internet Explorer 10)" src="http://srchea.com/blog/wp-content/uploads/2012/11/pure-css3-logo-ie10-300x80.png" alt="Pure CSS3 logos (Internet Explorer 10)" width="300" height="80" />version 10</a></p>
<h1>Debugging/Helping tools<a name="p3"></a></h1>
<p>I built those logos manually, no export from files or anything. There are a lot of great tools to set gradients, here is a list of generators that I used:</p>
<ul>
<li><a href="http://ie.microsoft.com/testdrive/graphics/cssgradientbackgroundmaker/default.html" target="_blank">CSS Gradient Background Maker</a></li>
<li><a href="http://www.colorzilla.com/gradient-editor/" target="_blank">ColorZilla</a></li>
</ul>
<p>For positioning I have simply used Firebug and the native DOM explorer of Chrome. :)</p>
<p>As usual, you can find the source here: <a href="https://github.com/srchea/Pure-CSS3-logos" target="_blank">https://github.com/srchea/Pure-CSS3-logos</a></p>
]]></content:encoded>
			<wfw:commentRss>http://srchea.com/blog/2012/11/making-of-pure-css3-logos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft, Mozilla and W3C Talks on Mobile Web Apps</title>
		<link>http://srchea.com/blog/2012/08/microsoft-mozilla-and-w3c-talks-on-mobile-web-apps/</link>
		<comments>http://srchea.com/blog/2012/08/microsoft-mozilla-and-w3c-talks-on-mobile-web-apps/#comments</comments>
		<pubDate>Mon, 06 Aug 2012 20:16:58 +0000</pubDate>
		<dc:creator>Sann-Remy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Conferences]]></category>
		<category><![CDATA[HTML5/CSS3]]></category>

		<guid isPermaLink="false">http://srchea.com/blog/?p=90</guid>
		<description><![CDATA[Here I was, the W3café event took place in the conference center of Microsoft France on June 29, 2012. I admit this post comes a bit late but interesting things were discussed during this event. The main subject was: Mobile applications build with HTML5. During this Friday afternoon, there were 2 different talks including &#8220;Internet [...]]]></description>
				<content:encoded><![CDATA[<p>Here I was, the <a title="W3café on Web standards (fr)" href="http://france.w3cafe.org/w3cafe-paris/w3cafe-standards-web-du-29-06-12/">W3café event</a> took place in the conference center of Microsoft France on June 29, 2012. I admit this post comes a bit late but interesting things were discussed during this event. The main subject was: Mobile applications build with HTML5.</p>
<p>During this Friday afternoon, there were 2 different talks including &#8220;Internet Explorer 10 and development with HTML5 on Windows 8&#8243; and &#8220;Mozilla and the Mobile Web: from Firefox for Android to Boot to Gecko&#8221;. Between both talks, we had to choose a workshop: I took the &#8220;Creation of a Mobile Web app based on HTML5&#8243; introduced by a W3C member, Dominique Hazael-Massieux.</p>
<p><iframe class="gmaps" src="http://maps.google.fr/maps?cid=0,0,3219543102930580332&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe></p>
<p>Here is the list of talks that I attended:</p>
<ul>
<li>IE10 and development with HTML5 on Windows 8<br />
by <a title="David Rousset's blog on MSDN" href="http://blogs.msdn.com/b/davrous/" target="_blank">David Rousset</a> (Microsoft France)</li>
<li>Creation of a Mobile Web app based on HTML5<br />
by <a title="Dominique Hazael-Massieux's W3C page" href="http://www.w3.org/People/Dom/" target="_blank">Dominique Hazael-Massieux</a> (W3C)</li>
<li>Mozilla and the Mobile Web: from Firefox for Android to Boot to Gecko<br />
by <a title="Tristan Nitot's blog on Mozilla" href="http://blog.mozilla.org/beyond-the-code/">Tristan Nitot</a> (Mozilla Europe)</li>
</ul>
<p>I wrote a summary of hot topics that have been discussed during these conferences:</p>
<ol>
<li><a href="http://srchea.com/blog/2012/08/microsoft-mozilla-and-w3c-talks-on-mobile-web-apps/#p1">Internet Explorer 10 for autumn 2012, what&#8217;s new?</a></li>
<li><a href="http://srchea.com/blog/2012/08/microsoft-mozilla-and-w3c-talks-on-mobile-web-apps/#p2">Mobile: Web app vs. Native app</a></li>
<li><a href="http://srchea.com/blog/2012/08/microsoft-mozilla-and-w3c-talks-on-mobile-web-apps/#p3">Mozilla&#8217;s B2G and Marketplace</a></li>
</ol>
<p><span id="more-90"></span></p>
<h1>Internet Explorer 10 for autumn 2012, what&#8217;s new?<a name="p1"></a></h1>
<p>Internet Explorer 10 will be only supported on Windows 7, Windows 8 and Windows Phone 8. The release date is scheduled for October/November 2012, it will come out along with Windows 8. A good point is that updates will come more frequently for public users. Let&#8217;s see what IE10 has inside!</p>
<p><strong>Responsive design and User Experience improvements</strong></p>
<p>First of all, it works with 2 different user experience modes:</p>
<ol>
<li>Desktop mode: the classic mode, it supports all plugins. This doesn&#8217;t differ from classic browsers and the user experience is very similar to IE9.</li>
<li><del>Metro</del> Windows 8-style UI mode: it enables the browser inside the desktop. The bad point is that it does not support plug-ins but partially Flash and Silverlight. This mode allows Windows 8 users to improve their experiences with other <del>Metro</del> Windows 8-style UI apps.</li>
</ol>
<p>Windows 8 integrates a JS Framework for <del>Metro </del>Windows 8-style UI app developments called WinJS: UI, transitions, events, and other set of controls like a date picker, a list, a rating component, etc.</p>
<p>IE10 now supports the CSS3 Grids, Regions and Exclusions including the <code>wrap-flow</code> property. This makes mobile and Web integrations easier thank to the flexible page layout. Here is <a title="CSS3 Regions demo" href="http://umaar.github.com/css-region-demo/" target="_blank">a good demo</a> of what regions are (resize your window).</p>
<p><strong>HTML5/CSS3 and tablet features support</strong></p>
<p>For me, the biggest improvement is the support of WebSockets and WebWorkers. We can wonder why they took so much time, and the answer is that they wanted to be sure that the specs are finalized.</p>
<p>Web game developers should be glad with the hardware acceleration support, that&#8217;s a big step for anyone who develop with WebGL. Moreover, all CSS3 transitions are handled by the hardware acceleration.</p>
<p>For tablets and mobiles, <a title="W3C Touch Events" href="http://www.w3.org/TR/touch-events/" target="_blank">touch events</a> are now supported. Actually, finger touch, pen, and mouse events became generic pointer events. Microsoft has gathered those events to not compromise mouse events: they called it <a title="MSPointer events" href="http://blogs.msdn.com/b/ie/archive/2011/09/20/touch-input-for-ie10-and-metro-style-apps.aspx" target="_blank">MSPointer events</a>. However, this <a title="Getting touchy about patents" href="http://blog.jquery.com/2012/04/10/getting-touchy-about-patents/" target="_blank">topic has been discussed</a>, because some Apple&#8217;s patents have been disclosed, and subsequently it has caused a break in the W3C&#8217;s specs.</p>
<p>Other features are supported such as offline storage types, drag and drop, etc:</p>
<ul>
<li><a title="The localStorage on W3C" href="http://www.w3.org/TR/webstorage/#the-localstorage-attribute" target="_blank">localStorage</a>: a simple key/value store map</li>
<li><a title="IndexedDB on W3C" href="http://www.w3.org/TR/IndexedDB/" target="_blank">IndexedDB</a>: a document-oriented database (NoSQL)</li>
<li><a title="Drag and Drop on W3C" href="http://dev.w3.org/html5/spec/dnd.html" target="_blank">Drag and Drop</a>: handlers and events for draggable elements</li>
</ul>
<p>To ensure the compatibility of old Web sites, the quirks mode is implemented until IE9. The consequence is that when the DOCTYPE is not defined, some markup are not supported: For example, in the quirks mode, the <code>&lt;canvas&gt;</code> is not supported. IE10 finally drops the quirks mode for standards and almost standards modes. You will find more details on how the rendering works on <a title="Activating Browser Mode with Doctype" href="http://hsivonen.iki.fi/doctype/">this page</a>.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-101" title="David Rousset introducing Internet Explorer 10 at the W3café 2012" src="http://srchea.com/blog/wp-content/uploads/2012/07/w3cafe-2012-ie10.jpg" alt="David Rousset introducing Internet Explorer 10 at the W3café 2012" width="650" height="488" /></p>
<p>To put it in a nutshell, Microsoft has done a lot of improvements for its browser with a many HTML5 features support, especially for game developers. On the one hand, IE10 may regain the market against Chrome and Firefox. On the other hand, we didn&#8217;t see any performance benchmark to compare the real place of IE10 between its current competitors.</p>
<h1>Mobile: Web app vs. Native app<a name="p2"></a></h1>
<p>Developing a mobile Web application needs another approach than a classic Web application development. Many parameters have to be considered like different interactions including user experiences. Screen sizes, resolutions, network latencies, CPU (and obviously, batteries), OS versions, phone brands have to be taken in consideration… At the opposite of a desktop, a mobile device is more personal and social: the relation between its owner is stronger.</p>
<p>Native applications don’t make users interconnected: You can not share a view with others (by sharing an URL). Moreover, it depends on OS versions and stores that provide the app whereas Web applications keep users safe: personal data are isolated from applications, like address books, phone numbers. And over all, no installation is needed and it gives easier access: get the URL by a QR code. For instance, a mall which provides its map via a Web app is more encouraging for the user to get it than to download it from a store, then to accept the terms of service, to install it and to use it finally.</p>
<p>However, there are inconveniences to design a Web app: as Dominique Hazael-Massieux told us, the screen size depends on phone models but now, we can find the same screen size (or almost) but different resolutions. That is what Apple has introduced with their mobile device products. Here is <a title="A pixel is not a pixel is not a pixel" href="http://www.quirksmode.org/blog/archives/2010/04/a_pixel_is_not.html" target="_blank">a good explanation of the problem</a> written by Peter-Paul Koch, who entitled it: &#8220;A pixel is not a pixel is not a pixel&#8221;. An mentioned solution is to use the <code>viewport</code> meta tag but this is not a standard yet.</p>
<p><strong>Smaller inputs but new sensors</strong></p>
<p>On a mobile device, inputs are usually small. The typing is slower and longer than on a PC keyboard. The W3C is up to normalize <a title="Speech input (API draft)" href="http://lists.w3.org/Archives/Public/public-xg-htmlspeech/2011Feb/att-0020/api-draft.html" target="_blank">the speech input</a>, but for this moment, only the webkit-based can use this feature with the <code>x-webkit-speech</code> attribute. Many other attributes is coming up to help the mobile user such as <code>tel</code>, <code>email</code>, <code>date</code>, <code>url</code>, etc. You can find the whole list of these new input controls <a title="Input controls (W3C)" href="http://www.w3.org/TR/html-markup/input.html" target="_blank">here</a>.</p>
<p><strong>Optimization advises</strong></p>
<p><strong></strong>Dominique gave us some precious advises on how to optimize a Web application, especially tips to save bandwidth. Nowadays, the bandwidth is paid by users, every bit is expensive and need to be saved (due to limited 3G/4G data plan, expensive roaming):</p>
<ul>
<li>Minify every static files: JS, CSS to save bits and to get a faster execution.</li>
<li>Use HTTP headers such as the <code>Cache-Control</code>, <code>Last-Modified</code>&#8230;</li>
<li>Minimize images into CSS sprites to avoid many connections.</li>
<li>Check the EXIF of photos, sometime they are useless.</li>
<li>Be careful with JS libraries, they are heavy and sometime they are not fully used.</li>
<li>Build CSS files into a single.</li>
<li>Control cookies: they are sent at every HTTP request to the same domain name.</li>
<li>Use the offline storage offered by HTML5: <code>localStorage</code>, <code>IndexedDB</code>, <code>requestFileSystem</code> (Chrome only), <code>&lt;html&gt; manifest</code> attribute&#8230;</li>
</ul>
<p>To limit network connections, let the client do the more but keep in mind that the CPU and the memory cost a lot for the battery.</p>
<p>He also talked about the WebRTC: Real-Time Communication. WebRTC has the aim to make traditional desktop application such as P2P, voice calling into Web applications.</p>
<p style="text-align: center;"><img class="size-full wp-image-103 aligncenter" title="Dominique Hazael-Massieux at the W3café 2012" src="http://srchea.com/blog/wp-content/uploads/2012/07/w3cafe-2012-html5.jpg" alt="Dominique Hazael-Massieux at the W3café 2012" width="650" height="488" /></p>
<p>This picture above relates the show case of an augmented reality application using WebGL and WebRTC technologies.</p>
<h1>Mozilla&#8217;s B2G and Marketplace<a name="p3"></a></h1>
<p>The last part was for Tristan Nitot, the president of Mozilla Europe. He introduced <a title="Boot to Gecko (B2G)" href="https://wiki.mozilla.org/B2G" target="_blank">Boot to Gecko (B2G)</a> and the <a title="Mozilla Marketplace" href="https://marketplace.mozilla.org/" target="_blank">Mozilla Marketplace</a> which wants to be more a open store than existing ones. B2G also wants to be a new OS for mobile device and to use 100% Web apps. Mozilla wish to extend all sensors to Web APIs, so that, every apps could handle them directly from HTML5. Its architecture is composed of a Linux kernel, Gecko and Web apps.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-102" title="Tristan Nitot demonstrating Mozilla B2G on a Samsung Galaxy S II" src="http://srchea.com/blog/wp-content/uploads/2012/07/w3cafe-2012-b2g.jpg" alt="Tristan Nitot demonstrating Mozilla B2G on a Samsung Galaxy S II" width="650" height="488" /></p>
<p>At the end, he showed us a demonstration of a WebGL application which runs on B2G on a Samsung Galaxy S II. It was pretty stunning!</p>
]]></content:encoded>
			<wfw:commentRss>http://srchea.com/blog/2012/08/microsoft-mozilla-and-w3c-talks-on-mobile-web-apps/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Set up Belkin F5D7051 on Ubuntu</title>
		<link>http://srchea.com/blog/2012/04/set-up-belkin-f5d7051-on-ubuntu/</link>
		<comments>http://srchea.com/blog/2012/04/set-up-belkin-f5d7051-on-ubuntu/#comments</comments>
		<pubDate>Sun, 15 Apr 2012 16:33:39 +0000</pubDate>
		<dc:creator>Sann-Remy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Desktop]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://srchea.com/blog/?p=79</guid>
		<description><![CDATA[This is an unusual post but it might help few people who are trying to set up Wi-Fi connection with a Belkin F5D7051. This was tested on Ubuntu 11.10 32-bit. Cause During the installation of Ubuntu, the Wi-Fi has been successfully connected but after rebooting: no network interface (wlan0). This is due to a conflict [...]]]></description>
				<content:encoded><![CDATA[<p>This is an unusual post but it might help few people who are trying to set up Wi-Fi connection with a Belkin F5D7051. This was tested on Ubuntu 11.10 32-bit.</p>
<p class="center"><img src="http://srchea.com/blog/wp-content/uploads/2012/04/Belkin_F5D7051.jpg" alt="Belkin F5D7051 Wi-Fi USB dongle" title="Belkin F5D7051 Wi-Fi USB dongle" width="186" height="138" class="alignnone size-full wp-image-80" /></p>
<h2>Cause</h2>
<p>During the installation of Ubuntu, the Wi-Fi has been successfully connected but after rebooting: no network interface (wlan0). This is due to a conflict between 2 modules: <code>rt2500usb</code> and <code>rndis_wlan</code>. In fact, there are 2 types of F5D7051 based on the Broadcom BCM4320 and Ralink RT2500USB chipsets. The problem is that both have the same ID vendor:product that is to say <code>050d:7051</code>, so the kernel could load the wrong one. Here are the steps to fix the issue.</p>
<h2>Troubleshoot</h2>
<p>The following solution is to blacklist the wrong module.</p>
<ol>
<li>Ensure that you have the right component by typing in the terminal:

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">lsusb</pre></td></tr></table></div>

<p>It should display something like:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="properties" style="font-family:monospace;">Bus 001 Device 003: ID 050d:<span style="">7051</span> Belkin Components F5D7051
802.11g Adapter v1000 <span style="">&#91;</span>Broadcom <span style="">4320</span> USB<span style="">&#93;</span></pre></td></tr></table></div>

<p>Note that <code>050d:7051</code> is the unique identifier of the component and <code>[Broadcom 4320 USB]</code> is the based chipset.
</li>
<li>Check loaded modules with:

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">lsmod</span></pre></td></tr></table></div>

<p>If you see both modules: <code>rt2500usb</code> and <code>rndis_wlan</code>, edit the <code>/etc/modprobe.d/blacklist.conf</code> and add the following line at the end of the file (to edit you need to be root; if you have a Ralink chipset, replace <code>rt2500usb</code> by <code>rndis_wlan</code>):</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="properties" style="font-family:monospace;">blacklist rt2500usb</pre></td></tr></table></div>

<p>Then, unload the module by typing in the terminal:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> modprobe <span style="color: #660033;">-r</span> rt2500usb</pre></td></tr></table></div>

<p>Finally, save the file and reboot the computer.</li>
</ol>
<p>Hope it helps. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://srchea.com/blog/2012/04/set-up-belkin-f5d7051-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Terrain generation: the diamond-square algorithm and Three.js</title>
		<link>http://srchea.com/blog/2012/02/terrain-generation-the-diamond-square-algorithm-and-three-js/</link>
		<comments>http://srchea.com/blog/2012/02/terrain-generation-the-diamond-square-algorithm-and-three-js/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 15:01:45 +0000</pubDate>
		<dc:creator>Sann-Remy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[WebGL]]></category>

		<guid isPermaLink="false">http://srchea.com/blog/?p=58</guid>
		<description><![CDATA[Well, I get started learning 3D concepts/coding, so this is all new for me. I&#8217;ve done a first try to build an application that runs in WebGL-enabled browsers. After spending some days on it, I finally have something fit to be seen. This post deals with how I&#8217;ve made that little random terrain generation application [...]]]></description>
				<content:encoded><![CDATA[<p>Well, I get started learning 3D concepts/coding, so this is all new for me. I&#8217;ve done a first try to build an application that runs in WebGL-enabled browsers. After spending some days on it, I finally have something fit to be seen.</p>
<p>This post deals with how I&#8217;ve made that little <a title="Terrain generation application (live demo)" href="http://srchea.com/apps/terrain-generation-diamond-square-threejs-webgl/">random terrain generation application</a> using the <a title="Three.js on GitHub" href="https://github.com/mrdoob/three.js/" target="_blank">Three.js framework</a> and the <a title="The explaination of the diamond-square algorithm" href="http://en.wikipedia.org/wiki/Diamond-square_algorithm" target="_blank">diamond-square algorithm</a>. Obviously, this works with the new generation of web-browsers (with WebGL support). I have tested it on Firefox 9, 10 and Chrome 16, 17. Sorry for Internet Explorer fans, that app doesn&#8217;t support because it uses the WebGL context (perhaps switching in canvas rendering would work, I haven&#8217;t tested yet).</p>
<p style="text-align: center;"><a title="Terrain generation application (live demo)" href="http://srchea.com/apps/terrain-generation-diamond-square-threejs-webgl/"><img class="aligncenter" title="Random terrain generation application" src="http://srchea.com/blog/wp-content/uploads/2012/02/terrain-generation-screenshot-1.jpg" alt="Random terrain generation application" /><strong><br />
Go to the live demo page</strong></a></p>
<p>You can run the live demo at <a title="Terrain generation application (live demo)" href="http://srchea.com/apps/terrain-generation-diamond-square-threejs-webgl/">this address</a>. As always, you can find the <a title="The source code of the application" href="https://github.com/srchea/Terrain-Generation" target="_blank">source code</a> on <a title="srchea on GitHub" href="https://github.com/srchea/" target="_blank">my GitHub profile</a>.<br />
Here are some screenshots of what <a title="Terrain generation application (live demo)" href="http://srchea.com/apps/terrain-generation-diamond-square-threejs-webgl/">the app</a> can do but not only:</p>
<p style="text-align: center;"><a href="http://srchea.com/blog/wp-content/uploads/2012/02/terrain-generation-screenshot-3.jpg"><img class="alignnone size-medium wp-image-61" title="A random terrain with a texture" src="http://srchea.com/blog/wp-content/uploads/2012/02/terrain-generation-screenshot-3-300x162.jpg" alt="A random terrain with a texture" width="300" height="162" /></a><a href="http://srchea.com/blog/wp-content/uploads/2012/02/terrain-generation-screenshot-2.jpg"><img class="alignnone size-medium wp-image-60" title="Random terrain with wireframes (mesh)" src="http://srchea.com/blog/wp-content/uploads/2012/02/terrain-generation-screenshot-2-300x162.jpg" alt="Random terrain with wireframes (mesh)" width="300" height="162" /></a></p>
<p>Of course, the application gives us more features than applying a texture or showing a mesh. I invite you to discover and to play with it. By the way, I would really appreciate your feedback. :-)</p>
<ol>
<li><a href="http://srchea.com/blog/2012/02/terrain-generation-the-diamond-square-algorithm-and-three-js/#p1">The diamond-square algorithm</a></li>
<li><a href="http://srchea.com/blog/2012/02/terrain-generation-the-diamond-square-algorithm-and-three-js/#p2">The scene</a>
<ol>
<li><a href="http://srchea.com/blog/2012/02/terrain-generation-the-diamond-square-algorithm-and-three-js/#p2-1">Working with shapes, vertices, meshes and textures</a></li>
<li><a href="http://srchea.com/blog/2012/02/terrain-generation-the-diamond-square-algorithm-and-three-js/#p2-2">The camera</a></li>
<li><a href="http://srchea.com/blog/2012/02/terrain-generation-the-diamond-square-algorithm-and-three-js/#p2-3">The rendering</a></li>
</ol>
</li>
<li><a href="http://srchea.com/blog/2012/02/terrain-generation-the-diamond-square-algorithm-and-three-js/#p3">Changeable parameters</a></li>
<li><a href="http://srchea.com/blog/2012/02/terrain-generation-the-diamond-square-algorithm-and-three-js/#p4">Performance</a></li>
<li><a href="http://srchea.com/blog/2012/02/terrain-generation-the-diamond-square-algorithm-and-three-js/#p5">Bottom line</a></li>
</ol>
<p><span id="more-58"></span></p>
<h1>The diamond-square algorithm<a name="p1"></a></h1>
<p>As I said earlier, I have used the diamond-square algorithm to generate a random fractal terrain. If you already know how the midpoint displacement algorithm works (what if you don&#8217;t too), the diamond-square algorithm would be pretty easy to understand. <a title="The explaination of some terrain generation algorithms" href="http://gameprogrammer.com/fractal.html" target="_blank">Here is a good explanation</a> of these algorithms.</p>
<p>First of all, that algorithm only works on 2D arrays of 2<sup>n</sup>+1 dimensions (e.g. 129&#215;129, 1025&#215;1025, etc.). As its name suggests, it works on squares and it needs the four corner points, and the midpoint to generate height values regarding average values of corners. After that, it takes the middle of each edges (it actually builds diamonds) and it takes the midpoint to get other squares (sub-squares). <a title="The diamond-square step by step" href="http://paulboxley.com/blog/2011/03/terrain-generation-mark-one" target="_blank">This excellent post by Paul Boxley</a> shows the behavior of the algorithm step by step.</p>
<p>Moreover, it exists some other algorithms of terrain generations that we can mention like the Perlin noise, widely used in computer games and movies such as Tron.</p>
<h1>The scene<a name="p2"></a></h1>
<p>In this part, I explain how I have basically made the scene. It contains 2 main elements: a polygon mesh for the terrain and a perspective camera. Besides, it has a control element for the camera, but this will be explained in the camera section.</p>
<h2>Working with shapes, vertices, meshes and textures<a name="p2-1"></a></h2>
<p>I have used a plane (PlaneGeometry object) for the terrain grid. That shape offers the possibility to modify the z axis via their vertices.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">geometry</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">PlaneGeometry</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">width</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">height</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">segments</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">segments</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> index <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">segments</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">segments</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">geometry</span>.<span style="color: #660066;">vertices</span><span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">position</span>.<span style="color: #660066;">z</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">terrain</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        index<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>For the texture, Three.js gives the MeshBasicMaterial object to set an image on the mesh with the &#8216;map&#8217; attribute. Otherwise, we can put the &#8216;wireframe&#8217; and its &#8216;color&#8217;.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">texture</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">material</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">MeshBasicMaterial</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        map<span style="color: #339933;">:</span> THREE.<span style="color: #660066;">ImageUtils</span>.<span style="color: #660066;">loadTexture</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">texture</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">material</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">MeshBasicMaterial</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        color <span style="color: #339933;">:</span> 0x000000<span style="color: #339933;">,</span>
        wireframe <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Subsequently, we build the mesh with its shape and its texture. Then we add it in the scene.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">mesh</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Mesh</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">geometry</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">material</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">scene</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">mesh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>For aesthetic and performance reasons, we can add an fog effect in the whole scene. In fact, the CPU/GPU doesn&#8217;t have to compute all the terrain to the end but the viewable part. This can be added with:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">scene</span>.<span style="color: #660066;">fog</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">FogExp2</span><span style="color: #009900;">&#40;</span>0xffffff<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fog</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The way of fading fog with distance is controlled by the Exponential Squared mode.</p>
<h2>The camera<a name="p2-2"></a></h2>
<p>The camera that I have used is the perspective projection camera. It is defined by its field of view (fov), aspect ratio, near plane and far plane. In few words, this is similar to the human view.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fov</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">aspect</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">innerWidth</span><span style="color: #339933;">/</span>window.<span style="color: #660066;">innerHeight</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">near</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">far</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">100000</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">camera</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">PerspectiveCamera</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fov</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">aspect</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">near</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">far</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If you used to play at Call Of Duty, Battlefield, Unreal Tournament, Counter-Strike or you are a FPS gamer, you will find your feet. In fact, the camera is controlled exactly like in a FPS game with just one difference: you need to drag the screen to change the angle. The first person controls offered by the framework doesn&#8217;t handle correctly what I would like to have, especially for the mouse dragging. However, that lets us to use the keyboard for navigation using the native behavior of the FirstPersonControls (native controls from Three.js), that is to say arrow keys or a (strafe left), w (move up), d (strafe right), s (move down) and r (go upper), f (go lower). So, I have made my own controls, called: FirstPersonNavigationControls.</p>
<p>First of all, there are some attributes of the FirstPersonControls that need to be set to allow the FirstPersonNavigationControls to have the wished behavior such as vertical constrains.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstPersonControls</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">FirstPersonControls</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">object</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">domElement</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstPersonControls</span>.<span style="color: #660066;">movementSpeed</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5.0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstPersonControls</span>.<span style="color: #660066;">lookSpeed</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.005</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstPersonControls</span>.<span style="color: #660066;">noFly</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstPersonControls</span>.<span style="color: #660066;">activeLook</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstPersonControls</span>.<span style="color: #660066;">constrainVertical</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstPersonControls</span>.<span style="color: #660066;">verticalMin</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstPersonControls</span>.<span style="color: #660066;">verticalMax</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>For the mouse dragging, I have used the longitude/latitude system. It rotates the camera regarding the z axis from 0° to 360° (modulo) and the y axis at 180° (between -90° and 90°).</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>._lat <span style="color: #339933;">=</span> <span style="">Math</span>.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="">Math</span>.<span style="color: #660066;">min</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>._lat<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>._phi <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #CC0000;">90</span><span style="color: #339933;">-</span><span style="color: #000066; font-weight: bold;">this</span>._lat<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #339933;">/</span><span style="color: #CC0000;">180</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>._theta <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._lon <span style="color: #339933;">*</span> <span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #339933;">/</span><span style="color: #CC0000;">180</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">object</span>.<span style="color: #660066;">target</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="">Math</span>.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._phi<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">cos</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._theta<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">object</span>.<span style="color: #660066;">target</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> <span style="">Math</span>.<span style="color: #660066;">cos</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._phi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">object</span>.<span style="color: #660066;">target</span>.<span style="color: #660066;">z</span> <span style="color: #339933;">=</span> <span style="">Math</span>.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._phi<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._theta<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>After normalizing x, y, z almost like in <a title="The Matrix4.js file in Three.js" href="https://github.com/mrdoob/three.js/blob/master/src/core/Matrix4.js" target="_blank">the lookAt() method</a>, we obtain the rotation matrix that we apply on the camera. The whole code of the FirstPersonNavigationControls class can be found here.</p>
<h2>The rendering<a name="p2-3"></a></h2>
<p>The scene is rendered in the WebGL context and frames updating are fully handled by the framework with the requestAnimationFrame() and render() functions.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">renderer</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> THREE.<span style="color: #660066;">WebGLRenderer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> antialias<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">function</span> animate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    requestAnimationFrame<span style="color: #009900;">&#40;</span>animate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    scene.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h1>Changeable parameters<a name="p3"></a></h1>
<p>The live demo gives a control panel, here is the list of parameters.<br />
Variables that directly affect the terrain:</p>
<ol>
<li><strong>Size</strong> (width, height)<br />
The size of the terrain in pixel.</li>
<li><strong>Number of segments</strong> (2<sup>n</sup>+1 dimensions)<br />
Note that the more segments you will set, the more computing time it will take.</li>
<li><strong>Smoothing factor</strong><br />
This affects random height variations. The more is the value, the more is the height variation.</li>
</ol>
<p>Others that affect the scene, without regenerating the terrain:</p>
<ol>
<li><strong>Textures</strong><br />
You can choose between 3 distinct textures or none: it puts black wireframes.</li>
<li><strong>Fog</strong><br />
The density of the fog used by the exponential squared function (Exp2).</li>
<li><strong>Border</strong><br />
This enables the border on the edges of the terrain.</li>
</ol>
<p>Debug/performance:</p>
<ol>
<li><strong>Info</strong><br />
If checked, this displays the camera position/angle and the number of frames per second (FPS) that the browser computes.</li>
</ol>
<h1>Performance<a name="p4"></a></h1>
<p>On the one hand, I have run the application on my laptop under Windows 7, Intel core 2 duo 2.1 GHz and 4 Gb of RAM with Firefox 10 and Chrome 16. Both work with 60 FPS, which is totally correct. On the other hand, I have executed it on my AMD64 1.3 GHz, 3 Gb of RAM using Ubuntu 10.10 and Chrome 17, I got 56 FPS. However, on the same computer, I have some issues to run with Firefox 10: it doesn&#8217;t update frames.</p>
<h1>Bottom line<a name="p5"></a></h1>
<p>That application is not stable, it is still in development. I would really appreciate if you guys send me your performance/bug issues. And feel free to send me some screenshots of your amazing terrain generation. :-)</p>
<ul>
<li><a title="Terrain generation application (live demo)" href="http://srchea.com/apps/terrain-generation-diamond-square-threejs-webgl/">Random terrain generation (live demo)</a></li>
<li><a title="The source code of the application" href="https://github.com/srchea/Terrain-Generation" target="_blank">Source code on GitHub</a></li>
<li><a title="Three.js on GitHub" href="https://github.com/mrdoob/three.js/" target="_blank">Three.js framework</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://srchea.com/blog/2012/02/terrain-generation-the-diamond-square-algorithm-and-three-js/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Build a real-time application using HTML5 WebSockets</title>
		<link>http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/</link>
		<comments>http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 17:57:10 +0000</pubDate>
		<dc:creator>Sann-Remy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WebSocket]]></category>

		<guid isPermaLink="false">http://srchea.com/blog/?p=19</guid>
		<description><![CDATA[In this very first post on my new blog, I show you how I&#8217;ve carried out a simple push system using WebSocket specified by the RFC 6455 document. Unfortunately, I can&#8217;t bring you a live demo because of some hosting service restrictions that don&#8217;t allow me to create sockets. So, I worked on localhost with [...]]]></description>
				<content:encoded><![CDATA[<p>In this very first post on my new blog, I show you how I&#8217;ve carried out a simple push system using WebSocket specified by <a title="RFC6455 - WebSocket" href="http://tools.ietf.org/html/rfc6455" target="_blank">the RFC 6455 document</a>. Unfortunately, I can&#8217;t bring you a live demo because of some hosting service restrictions that don&#8217;t allow me to create sockets. So, I worked on localhost with my own computer using Ubuntu, Apache and PHP-CLI. Nevertheless, you can <a title="Download PHP Push WebSocket on GitHub" href="https://github.com/srchea/PHP-Push-WebSocket/zipball/blog_demo" target="_blank">download scripts</a> on <a title="srchea's GitHub" href="https://github.com/srchea/" target="_blank">my github account</a>.</p>
<p>You&#8217;ll find one of several basic implementations of a communication between clients and a server by WebSocket in PHP. I deliberately didn&#8217;t complicate the code (by adding other features or improvements) to make it easy to understand.</p>
<ol>
<li><a href="http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/#p1">How does a WebSocket basically work?</a></li>
<li><a href="http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/#p2">The server side</a>
<ol>
<li><a href="http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/#p2-1">Handshake</a></li>
<li><a href="http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/#p2-2">Unmasking/Encoding data frames</a></li>
<li><a href="http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/#p2-3">Pushing data</a></li>
</ol>
</li>
<li><a href="http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/#p3">The client side</a>
<ol>
<li><a href="http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/#p3-1">Receiving data</a></li>
<li><a href="http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/#p3-2">Leave the application</a></li>
</ol>
</li>
<li><a href="http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/#p4">More information</a></li>
</ol>
<p><span id="more-19"></span></p>
<h1>How does a WebSocket basically work?<a name="p1"></a></h1>
<p>WebSocket communications are managed by a server. Each client have to introduce itself by sending a handshake request via the WebSocket protocol (ws). The server accepts or not to open a socket connection by sending a handshake response. I think that schemas below are telling more than a long text, so let&#8217;s have a look on these pictures:</p>
<p align="center"><img class="aligncenter size-full wp-image-21" title="WebSocket - Handshake" src="http://srchea.com/blog/wp-content/uploads/2011/12/websocket-handshake.png" alt="WebSocket - Handshake" /></p>
<p>Only after the handshake acceptance, both sides can communicate. Many clients can be connected to the server. For some performance reasons, the limit of clients can be set before creating a new socket.</p>
<p align="center"><img class="aligncenter size-full wp-image-20" title="WebSocket - Clients and server" src="http://srchea.com/blog/wp-content/uploads/2011/12/websocket-clients-server.png" alt="WebSocket - Clients and server" /></p>
<h1>The server side<a name="p2"></a></h1>
<p>Like I said earlier in this post, I used Apache 2 and PHP-CLI 5.3 under Ubuntu 10.10. In this section, we&#8217;re going to see some tricky parts of the server side especially the handshake method, encoding and unmasking data methods. And to finish, I&#8217;m going to explain the part of pushing data to clients. The server works like a daemon and is executed by the following command: <code>php -q phppushwebsocket.php</code></p>
<h2>Handshake<a name="p2-1"></a></h2>
<p>First of all, the handshake has to be done to let both, client and server, communicate. The browser introduces itself by sending HTTP headers, something like:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="properties" style="font-family:monospace;">GET / HTTP/<span style="">1.1</span>
Upgrade: websocket
Connection: Upgrade
Host: 127.0.0.1:<span style="">5001</span>
Origin: http://localhost
Sec-WebSocket-Key: k2towQT28s50DtKptTjZbg<span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">=</span>
Sec-WebSocket-Version: <span style="">13</span></pre></td></tr></table></div>

<p>The Sec-WebSocket-Version determines the protocol version of the connection. The HTTP Headers could differ regarding the version. That&#8217;s why we make sure that the information that we want to extract are correct. For instance, Origin was Sec-WebSocket-Origin in the version 8. Moreover, server responses are not the same regarding versions. In the following code, it deals with the version 13. At the moment I&#8217;m writing, the unique web-browser which supports this implementation is Google Chrome 16, others would be rejected.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Do the handshaking between client and server
 * @param $client
 * @param $headers
 */</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handshake<span style="color: #009900;">&#40;</span><span style="color: #000088;">$client</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Getting client WebSocket version...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/Sec-WebSocket-Version: (.*)<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$version</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The client doesn't support WebSocket&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Client WebSocket version is <span style="color: #006699; font-weight: bold;">{$version}</span>, (required: 13)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$version</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">13</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Extract header variables</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Getting headers...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/GET (.*) HTTP/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$root</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/Host: (.*)<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/Origin: (.*)<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$origin</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/Sec-WebSocket-Key: (.*)<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Client headers are:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>- Root: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>- Host: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>- Origin: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$origin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>- Sec-WebSocket-Key: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Generating Sec-WebSocket-Accept key...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$acceptKey</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$acceptKey</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sha1</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$acceptKey</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$upgrade</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;HTTP/1.1 101 Switching Protocols<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">.</span>
                   <span style="color: #0000ff;">&quot;Upgrade: websocket<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">.</span>
                   <span style="color: #0000ff;">&quot;Connection: Upgrade<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">.</span>
                   <span style="color: #0000ff;">&quot;Sec-WebSocket-Accept: <span style="color: #006699; font-weight: bold;">$acceptKey</span>&quot;</span><span style="color: #339933;">.</span>
                   <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">&quot;Sending this response to the client #{<span style="color: #006699; font-weight: bold;">$client-&gt;getId</span>()}:&quot;</span>
            <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$upgrade</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">socket_write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSocket</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$upgrade</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHandshake</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Handshake is successfully done!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">&quot;WebSocket version 13 required&quot;</span>
            <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;(the client supports version <span style="color: #006699; font-weight: bold;">{$version}</span>)&quot;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Unmasking/Encoding data frames<a name="p2-2"></a></h2>
<p>Well, the client can now send messages to the server and vice-versa. Sent messages have to be formatted in a special way. In fact, if you try to display them, they will be encrypted, so they have to be unmasked to be human readable. The RFC 6455 describes <a title="RFC6455 - WebSocket (unmask frame)" href="http://tools.ietf.org/html/rfc6455#section-5.2">the way to unmask</a> data.</p>
<p>An implementation of that description in PHP would be this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Unmask a received payload
 * @param $payload
 */</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> unmask<span style="color: #009900;">&#40;</span><span style="color: #000088;">$payload</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$payload</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #cc66cc;">127</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">126</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$masks</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$payload</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$payload</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">127</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$masks</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$payload</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$payload</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$masks</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$payload</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$payload</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$text</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> ^ <span style="color: #000088;">$masks</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">%</span><span style="color:#800080;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We have to encode the text before sending to the client. If a sent text is wrongly encoded, the client might close the connection or not correctly receive it. This is a basic implementation without encryption:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Encode a text for sending to clients via ws://
 * @param $text
 */</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> encode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// 0x1 text frame (FIN + opcode)</span>
    <span style="color: #000088;">$b1</span> <span style="color: #339933;">=</span> <span style="color: #208080;">0x80</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span><span style="color: #208080;">0x1</span> <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0x0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span>  <span style="color: #cc66cc;">125</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$length</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">65536</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #990000;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CCS'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">126</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">65536</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #990000;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CCN'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">127</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$header</span><span style="color: #339933;">.</span><span style="color: #000088;">$text</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Pushing data<a name="p2-3"></a></h2>
<p>Now that we can send/receive messages and each side can display them properly, we&#8217;re going to see the pushing part. First of all, the script creates one process for the server + <em>n</em> processes for <em>n</em> connected clients. We&#8217;re going to fork the server process to make it still accepts new clients and children processes will send data to clients. This method below have to be called after the handshake.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Start a child process for pushing data
 * @param unknown_type $client
 */</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> startProcess<span style="color: #009900;">&#40;</span><span style="color: #000088;">$client</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Start a child process&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$pid</span> <span style="color: #339933;">=</span> pcntl_fork<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pid</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'could not fork'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pid</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// process</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// we are the child</span>
        <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// push something to the client</span>
            <span style="color: #000088;">$seconds</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$client</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;I am waiting <span style="color: #006699; font-weight: bold;">{$seconds}</span> seconds&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$seconds</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Note that we set the PID of the Client object in the server process. When a client leaves the application, it will send a &#8216;quit&#8217; command to the server (more information in the client side section). The server will treat the message and will send a kill request to the process. However, we can make it in another way: instead of forking the server process, we could execute another program and kill it properly.</p>
<h1>The client side<a name="p3"></a></h1>
<p>This part is really easy. Nothing special to figure out other than the WebSocket initialization and events which handle WebSockets: &#8216;onopen&#8217;, &#8216;onclose&#8217; and &#8216;onerror&#8217;. We will see the &#8216;onmessage&#8217; event in the receiving data section. This is a basic client side implementation:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> ws <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> WebSocket<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ws://127.0.0.1:5001'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ws.<span style="color: #660066;">onopen</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    write<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Connection successfully opened (readyState '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">')'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
ws.<span style="color: #660066;">onclose</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>
        write<span style="color: #009900;">&#40;</span>
            <span style="color: #3366CC;">'Closing... The connection is going throught'</span>
            <span style="color: #339933;">+</span> <span style="color: #3366CC;">'the closing handshake (readyState '</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">')'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span>
        write<span style="color: #009900;">&#40;</span>
            <span style="color: #3366CC;">'Connection closed... The connection has been closed'</span>
            <span style="color: #339933;">+</span> <span style="color: #3366CC;">'or could not be opened (readyState '</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">')'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
        write<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Connection closed... (unhandled readyState '</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">')'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
ws.<span style="color: #660066;">onerror</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    terminal.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;li style=&quot;color: red;&quot;&gt;'</span><span style="color: #339933;">+</span>event.<span style="color: #660066;">data</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/li&gt;'</span>
    <span style="color: #339933;">+</span> terminal.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Receiving data<a name="p3-1"></a></h2>
<p>The client doesn&#8217;t have to make a request to the server to know if there are new data. When there is something new, the server will send automatically to the concerned client. That new data is handled by the &#8216;onmessage&#8217; event.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">ws.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    write<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Server says: '</span><span style="color: #339933;">+</span>msg.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Leave the application<a name="p3-2"></a></h2>
<p>When a client leaves, a &#8216;quit&#8217; message is sent on the before unload window event (window.onbeforeunload). This message is directly handled by the server.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #660066;">onbeforeunload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ws.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'quit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Subsequently, the server will execute something like:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$action</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;quit&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Killing a child process&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">posix_kill</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> SIGTERM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">console</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Process {<span style="color: #006699; font-weight: bold;">$client-&gt;getPid</span>()} is killed!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h1>More information<a name="p4"></a></h1>
<p>This is some useful links for more information and details about WebSockets:</p>
<ul>
<li><a title="RFC6455 - WebSocket" href="http://tools.ietf.org/html/rfc6455" target="_blank">The WebSocket Protocol, RFC 6455 on the IETF Website</a></li>
<li><a title="W3C - The WebSocket API" href="http://dev.w3.org/html5/websockets/" target="_blank">The WebSocket API on the W3C Website</a></li>
</ul>
<p>Feel free to copy and adapt <a title="Download PHP Push WebSocket on GitHub" href="https://github.com/srchea/PHP-Push-WebSocket/zipball/blog_demo" target="_blank">the code source of PHP Push WebSocket</a>.</p>
<p>In few years, we might say good bye to the pull technology like AJAX and optimize network performance by the way. :-)<br />
Stay tuned&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
	</channel>
</rss>
