<?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>Agrafix Webdesign Blog &#187; agrafix</title>
	<atom:link href="http://blog.agrafix.net/author/agrafix/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.agrafix.net</link>
	<description>Programmieren, Technik und Internet</description>
	<lastBuildDate>Wed, 25 Jan 2012 06:59:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C++ CGI und mySQL</title>
		<link>http://blog.agrafix.net/2012/01/c-cgi-und-mysql/</link>
		<comments>http://blog.agrafix.net/2012/01/c-cgi-und-mysql/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 06:55:08 +0000</pubDate>
		<dc:creator>agrafix</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[CGI]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[Projekte]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dslan]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[twlan]]></category>

		<guid isPermaLink="false">http://blog.agrafix.net/?p=544</guid>
		<description><![CDATA[Ein CGI-Programm in C++ zu schreiben ist &#8211; eigentlich &#8211; ziemlich einfach: #include &#60;iostream&#62; &#160; using namespace std; &#160; int main&#40;&#41; &#123; cout &#60;&#60; &#34;Content-Type: text/html&#34; &#60;&#60; endl &#60;&#60; endl; &#160; cout &#60;&#60; &#34;&#60;html&#62;&#60;head&#62;&#60;title&#62;Hello CGI&#60;/title&#62;&#60;/head&#62;&#60;body&#62;&#34; &#60;&#60; endl; cout &#60;&#60; &#34;&#60;h1&#62;C++ says Hi to the CGI API&#60;/h1&#62;&#34; &#60;&#60; endl; cout &#60;&#60; &#34;&#60;/body&#62;&#60;/html&#62;&#34; &#60;&#60; endl; &#160; return [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2012/01/c-cgi-und-mysql/' addthis:title='C++ CGI und mySQL ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>
<strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2012/01/websocketserver/' rel='bookmark' title='WebSocketServer'>WebSocketServer</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Ein CGI-Programm in C++ zu schreiben ist &#8211; eigentlich &#8211; ziemlich einfach:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Content-Type: text/html&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello CGI&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;&lt;h1&gt;C++ says Hi to the CGI API&lt;/h1&gt;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;&lt;/body&gt;&lt;/html&gt;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Das ganze kann man jetzt durch einen C++ Compiler jagen, in das cgi-bin Verzeichniss des Webservers verschrieben, chmod +x, und dann zB unter http://webserver/cgi-bin/test.cgi ansehen. Nicht sehr spannend, da man diese statische Seite ja auch direkt in HTML schreiben hätte können und sich den Umweg über CGI sparen hätte können.</p>
<p>Interessant wird es also, wenn man mit C++ über CGI dynamische Webseiten (dynamisch nicht im Sinne von JavaScript, sondern eher ähnlich wie PHP) generieren möchte. Ein erster Schritt wäre hier, die GET-Variablen auszulesen. Die GET-Variablen werden von der CGI in die Environment-Variable <strong>QUERY_STRING</strong> geschrieben, und zwar in folgendem Format:<br />
<code>[key1]=[value1]&#038;[key2]=[value2]&#038;....&#038;[keyN]=[valueN]</code><br />
Wir müssen uns also eine Funktion zum Parsen dieses Strings schreiben:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> getVariables<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">unordered_map</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span>, std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000080;">&gt;</span> <span style="color: #000040;">&amp;</span>variables<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">// fetch the query string</span>
	<span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> raw_query_string <span style="color: #000080;">=</span> <span style="color: #0000dd;">getenv</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;QUERY_STRING&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// query string empty?</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>raw_query_string <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> 
	<span style="color: #008000;">&#123;</span>
		variables.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> variables<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">// key and value into a stringstream, init with an empty stream</span>
	<span style="color: #666666;">// to make sure get-queries such as ?step=1&amp;confirm are parsed </span>
	<span style="color: #666666;">// correctly</span>
	std<span style="color: #008080;">::</span><span style="color: #007788;">stringstream</span> key, value<span style="color: #008080;">;</span>
&nbsp;
	key.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	value.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// are we currently parsing a key or a value?</span>
	<span style="color: #0000ff;">bool</span> isKey <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// loop until we read all raw data</span>
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>raw_query_string <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #666666;">// if we read a &amp;, we'll change or type to key and store </span>
		<span style="color: #666666;">// are previous key and value variables into out map</span>
		<span style="color: #666666;">// and clear them</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>raw_query_string <span style="color: #000080;">==</span> <span style="color: #FF0000;">'&amp;'</span><span style="color: #008000;">&#41;</span> 
		<span style="color: #008000;">&#123;</span> 
			variables<span style="color: #008000;">&#91;</span>key.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> value.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			isKey <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span> 
			key.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			value.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #666666;">// if we read a =, we'll change or type to value</span>
		<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>raw_query_string <span style="color: #000080;">==</span> <span style="color: #FF0000;">'='</span><span style="color: #008000;">&#41;</span> 
		<span style="color: #008000;">&#123;</span> 
			isKey <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span> 
		<span style="color: #008000;">&#125;</span>
		<span style="color: #666666;">// anything else goes into the currently active stream</span>
		<span style="color: #0000ff;">else</span> 
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>isKey<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> key <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">*</span>raw_query_string<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
			<span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> value <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">*</span>raw_query_string<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #666666;">// move pointer to next char</span>
		raw_query_string<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">// store last key value pair, because query-strings </span>
	<span style="color: #666666;">// normaly don't end on &amp;</span>
	variables<span style="color: #008000;">&#91;</span>key.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> value.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> variables<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Das Problem ist, dass die <i>keys</i> und <i>values</i> URL-codiert sind, wenn man also Text verarbeiten möchte muss man sie noch entsprechend dekodieren. Das ist aber an dieser Stelle erstmal Aufgabe des Lesers <img src='http://blog.agrafix.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Natürlich möchte man jetzt nicht nur GET-Variablen lesen, sondern auch Daten empfangen können, die per POST geschickt werden. Diese schickt der WebServer an <em>stdin</em> des CGI-Scripts. Und leider nicht \0-terminiert, aber das ist kein Problem, da die Umgebungsvariable <b>CONTENT_LENGTH</b> die genaue Länge der POST-Daten enthält. Die POST Daten sind in gleichen Format wie die GET-Daten. Das Parsen funktioniert also entsprechend ähnlich, allerdings muss man erst beispielsweise per fread die Daten aus dem <i>stdin</i> in ein char-buffer lesen und \0-terminieren bevor man sie durch entsprechende parsing Funktion schicken kann.</p>
<p>Mit diesen zwei Funktionalitäten kann man schon eine Menge anstellen (wie zB ein Datei-basiertes Gästebuch), allerdings muss man beim Ausgeben von Benutzereingaben daran denken diese Entsprechend zu Maskieren, damit man keine XSS-Attacken und Ähnliches erlaubt. Dafür habe ich mir folgende kleine Hilfsfunktion geschrieben:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> htmlspecialchars<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> input<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	replaceAll<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&amp;&quot;</span>, <span style="color: #FF0000;">&quot;&amp;amp;&quot;</span>, input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	replaceAll<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&lt;&quot;</span>, <span style="color: #FF0000;">&quot;&amp;lt;&quot;</span>, input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	replaceAll<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&gt;&quot;</span>, <span style="color: #FF0000;">&quot;&amp;gt;&quot;</span>, input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	replaceAll<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;'&quot;</span>, <span style="color: #FF0000;">&quot;&amp;#039;&quot;</span>, input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	replaceAll<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>, <span style="color: #FF0000;">&quot;&amp;quot;&quot;</span>, input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> input<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> replaceAll<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> search, std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> replace, std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> <span style="color: #000040;">&amp;</span>str<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">size_t</span> currPos <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">size_t</span> findPos<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>findPos <span style="color: #000080;">=</span> str.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span>search, currPos<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #008080;">::</span><span style="color: #007788;">npos</span><span style="color: #008000;">&#41;</span> 
	<span style="color: #008000;">&#123;</span>
		str.<span style="color: #007788;">replace</span><span style="color: #008000;">&#40;</span>findPos, search.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, replace<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		currPos <span style="color: #000080;">=</span> findPos <span style="color: #000040;">+</span> replace.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Jetzt wird&#8217;s entlich interessant: Wir möchten in unserer C++ CGI-Anwendung natürlich auch (wie gewohnt aus PHP) eine mySQL-Datenbank ansprechen. Ich habe zuerst ziemlich lange mit dem MySQL Connector/C++ rumprobiert, hab das ganze allerdings nicht wirklich vernünftig zum Laufen bekommen (wenn Details gewünscht sind, kann ich gerne noch einen weiteren Blogpost dazu schreiben <img src='http://blog.agrafix.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> ). Viele Probleme die bei mir aufgetreten sind waren wohl nicht unbekannt und sind (teilweise) sogar ohne Lösung auf StackOverflow zu finden. Dort bin ich dann auch auf die Idee gekommen einfach die MySQL C-API zu nehmen. Gesagt getan, ich hab mir einen kleinen Wrapper dafür geschrieben und ab dort konnte ich ohne Probleme über C++ mit meiner mySQL Datenbank reden. Um die C-API zu benutzen, muss man einfach von der MySQL Homepage den &#8220;C-Connector&#8221; runterladen, das include-Directory im Compiler mit einbinden, das lib-Directory dem Linker geben und mit der libmysql linken. Dann kann man die Funktionen per #include &#8220;mysql.h&#8221; laden und verwenden. Je nach Projekt kann es nötig sein noch die SOCKET-Header (unter windows winsock2.h, unter linux sys/socket.h und arpa/inet.h) einzubinden.</p>
<p>Kleines Beispiel-Programm:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #339900;">#include &quot;mysql.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Content-Type: text/html&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;&lt;html&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;head&gt;&lt;title&gt;mySQL TEST&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	MYSQL <span style="color: #000040;">*</span>conn <span style="color: #000080;">=</span> mysql_init<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>mysql_real_connect<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;localhost&quot;</span>, <span style="color: #FF0000;">&quot;root&quot;</span>, <span style="color: #FF0000;">&quot;&quot;</span>, <span style="color: #FF0000;">&quot;testdatabase&quot;</span>, <span style="color: #0000ff;">NULL</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;mySQL Error #%u: %s &lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, mysql_errno<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span>, mysql_error<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>mysql_real_query<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;SELECT name, age FROM clients&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;mySQL Error #%u: %s &lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, mysql_errno<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span>, mysql_error<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;&lt;h1&gt;Client age table:&lt;/h1&gt;&lt;table&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;age&lt;/th&gt; &lt;/tr&gt;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	MYSQL_RES <span style="color: #000040;">*</span>result <span style="color: #000080;">=</span> mysql_store_result<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	MYSQL_ROW row<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>row <span style="color: #000080;">=</span> mysql_fetch_row<span style="color: #008000;">&#40;</span>result<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&lt;tr&gt;&lt;td&gt;%s&lt;/td&gt;&lt;td&gt;%d&lt;/td&gt;&lt;/tr&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, row<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, row<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	mysql_free_result<span style="color: #008000;">&#40;</span>result<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	mysql_close<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Da ich ja am TWLan-Projekt mitarbeite (<a href="http://www.twlan.org" title="TWLan">www.twlan.org</a>) habe ich als Test mal die Startseite und die Registration in C++ implementiert. Dafür habe ich noch eine kleine C++ HTML Template Klasse geschrieben, ist allerdings nichts besonderes. Nun habe ich die PHP-Startseite und die C++ CGI-Startseite mal (ohne Caching) gegeneinander im Punkto Geschwindigkeit gegenübergestellt:</p>
<pre>PHP-TWLan: 0.2501s 0.0743s 0.0771s 0.0693s 0.0672s 0.0635s 0.0775s
AVG:	   0.097s

CGI-TWLan: 0.0220s 0.0321s 0.0142s 0.0354s 0.0199s 0.0194s 0.0195s
AVG:	   0.023s</pre>
<p>Die CGI-Version ist, selbst bei nur 2-Datenbank-Abfragen, 4x schneller als der PHP-Code. Der sehr hohe Wert bei der ersten PHP-Zeitmessung erklärt sich übrigens dadurch, dass die Zend-Engine beim ersten durchlauf den PHP-Opcode cached. Ein solches Ergebnis war zu erwarten &#8211; und wenn man es dann mal selbst erlebt ist es doch sehr motivierend weitere Web-Anwendungen in C++ zu schreiben. Wenn man dann noch FastCGI unterstützt, und zB lighttpd als Webserver verwendet, dann hat man wirklich die ultimativ-schnelle Web-Anwendung <img src='http://blog.agrafix.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Das &#8220;TWLanCGI&#8221; Projekt gibts im TWLan-Forum übrigens für Windows zum Download (<a href="http://de.twlan.org/showthread.php?tid=4809" title="TWLan" target="_blank">hier</a>).</p>
<img src="http://blog.agrafix.net/?ak_action=api_record_view&id=544&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2012/01/c-cgi-und-mysql/' addthis:title='C++ CGI und mySQL ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><p><strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2012/01/websocketserver/' rel='bookmark' title='WebSocketServer'>WebSocketServer</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.agrafix.net/2012/01/c-cgi-und-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WebSocketServer</title>
		<link>http://blog.agrafix.net/2012/01/websocketserver/</link>
		<comments>http://blog.agrafix.net/2012/01/websocketserver/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 04:54:21 +0000</pubDate>
		<dc:creator>agrafix</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[LUA]]></category>
		<category><![CDATA[Projekte]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[WebSocket]]></category>

		<guid isPermaLink="false">http://blog.agrafix.net/?p=540</guid>
		<description><![CDATA[Gerade eben habe ich die erste öffentliche Version meines C++ WebSocketServers veröffentlicht. Es gibt eigentlich nicht viel dazu zu sagen, alle Informationen stehen auf der Webseite: WebSocketServer. Zusammengefasst ist es ein kleiner Server, der das HTML5-WebSocket Protokoll unterstützt. Um Anwendungen für den Server zu schreiben, kann man diese entweder in C++ oder in LUA einbauen! [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2012/01/websocketserver/' addthis:title='WebSocketServer ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>
<strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2012/01/c-lua-websocketserver/' rel='bookmark' title='C++ &amp; Lua WebSocketServer'>C++ &#038; Lua WebSocketServer</a></li>
<li><a href='http://blog.agrafix.net/2011/11/html5-websocket/' rel='bookmark' title='HTML5 WebSocket'>HTML5 WebSocket</a></li>
<li><a href='http://blog.agrafix.net/2011/12/managers-life-update/' rel='bookmark' title='Manager&#8217;s Life Update'>Manager&#8217;s Life Update</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Gerade eben habe ich die erste öffentliche Version meines C++ WebSocketServers veröffentlicht. Es gibt eigentlich nicht viel dazu zu sagen, alle Informationen stehen auf der Webseite: <a href="http://websocketserver.de/">WebSocketServer</a>. </p>
<p>Zusammengefasst ist es ein kleiner Server, der das HTML5-WebSocket Protokoll unterstützt. Um Anwendungen für den Server zu schreiben, kann man diese entweder in C++ oder in LUA einbauen!</p>
<img src="http://blog.agrafix.net/?ak_action=api_record_view&id=540&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2012/01/websocketserver/' addthis:title='WebSocketServer ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><p><strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2012/01/c-lua-websocketserver/' rel='bookmark' title='C++ &amp; Lua WebSocketServer'>C++ &#038; Lua WebSocketServer</a></li>
<li><a href='http://blog.agrafix.net/2011/11/html5-websocket/' rel='bookmark' title='HTML5 WebSocket'>HTML5 WebSocket</a></li>
<li><a href='http://blog.agrafix.net/2011/12/managers-life-update/' rel='bookmark' title='Manager&#8217;s Life Update'>Manager&#8217;s Life Update</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.agrafix.net/2012/01/websocketserver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ReTI IDE &amp; Simulator (Technische Informatik)</title>
		<link>http://blog.agrafix.net/2012/01/reti-ide-simulator-technische-informatik/</link>
		<comments>http://blog.agrafix.net/2012/01/reti-ide-simulator-technische-informatik/#comments</comments>
		<pubDate>Sat, 14 Jan 2012 12:29:29 +0000</pubDate>
		<dc:creator>agrafix</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Projekte]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[prozessor]]></category>
		<category><![CDATA[reti]]></category>
		<category><![CDATA[simulator]]></category>

		<guid isPermaLink="false">http://blog.agrafix.net/?p=533</guid>
		<description><![CDATA[Da ich aktuell an der Veranstaltung Technische Informatik an der Uni teilnehme war es im letzten Übungsblatt Aufgabe Programm-Code für einen einfachen Prozessor (ReTI oder auch RESA) zu schreiben. Das vorgeschlagene Tool dafür (Neumi) kann entsprechenden Programm-Code zwar super visualisieren, ist allerdings beim &#8220;coden&#8221; nicht sehr hilfreich da es kein vernünftiges Feedback bei Fehlern gibt [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2012/01/reti-ide-simulator-technische-informatik/' addthis:title='ReTI IDE &#38; Simulator (Technische Informatik) ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>
<em>Keine ähnlichen Beiträge vorhanden</em>]]></description>
			<content:encoded><![CDATA[<p>Da ich aktuell an der Veranstaltung Technische Informatik an der Uni teilnehme war es im letzten Übungsblatt Aufgabe Programm-Code für einen einfachen Prozessor (ReTI oder auch RESA) zu schreiben. Das vorgeschlagene Tool dafür (<a href="http://abs.informatik.uni-freiburg.de/teaching/Neumi/" title="Neumi">Neumi</a>) kann entsprechenden Programm-Code zwar super visualisieren, ist allerdings beim &#8220;coden&#8221; nicht sehr hilfreich da es kein vernünftiges Feedback bei Fehlern gibt und es auch keine gute Eingabemaske für den Code gibt. Deshalb hab ich mich hingesetzt und eine kleine HTML-JavaScript-IDE mit einer ReTI-Simulation in PHP zusammengebaut. Das ganze ist unter <a href="http://reti.agrafix.net/index.php" title="ReTI IDE &#038; Simulator">reti.agrafix.net</a> zu finden! Ich habe auch eine kleine Dokumentation dazu geschrieben, es sollte sich also alles von selbst erklären.<br />
Ein paar Worte vielleicht noch zur &#8220;Simulation&#8221;: Die Simulation ist eher ein Debugger, da der Code nicht assembliert sondern direkt interpretiert wird. Das ist notwendig um zielgenaues Feedback bei (Laufzeit-)Fehlern zu geben. Deshalb ist es nicht möglich auf Speicherzellen zuzugreifen, die Anweisungen/Befehle enthalten. (Mit dieser Einschränkung konnte man dennoch alle Aufgabenstellungen bis jetzt lösen <img src='http://blog.agrafix.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  ) Solange man das also nicht benötigt, kann der Simulator den korrekten Endspeicherzustand berechnen.</p>
<p><a href="http://reti.agrafix.net/" title="ReTI IDE &#038; Simulator">Zur ReTI IDE &#038; Simulator</a></p>
<p>Für die HTML-JavaScript-IDE bzw. dessen Code-Editor habe ich übrigens auf das <a href="http://codemirror.net/" title="CodeMirror">CodeMirror 2</a> Framework gebaut. Super Framework, leider muss man erstmal ein bisschen Dokumentation lesen bis man damit zu guten Ergebnisen kommt!</p>
<img src="http://blog.agrafix.net/?ak_action=api_record_view&id=533&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2012/01/reti-ide-simulator-technische-informatik/' addthis:title='ReTI IDE &amp; Simulator (Technische Informatik) ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><p><em>Keine ähnlichen Beiträge vorhanden</em></p>]]></content:encoded>
			<wfw:commentRss>http://blog.agrafix.net/2012/01/reti-ide-simulator-technische-informatik/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ &amp; Lua WebSocketServer</title>
		<link>http://blog.agrafix.net/2012/01/c-lua-websocketserver/</link>
		<comments>http://blog.agrafix.net/2012/01/c-lua-websocketserver/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 12:41:51 +0000</pubDate>
		<dc:creator>agrafix</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[LUA]]></category>
		<category><![CDATA[Projekte]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[WebSocket]]></category>

		<guid isPermaLink="false">http://blog.agrafix.net/?p=529</guid>
		<description><![CDATA[Hi, ich möchte eigentlich nur ein kurzes Update zu meinem Projekt C++ &#038; Lua WebSocketServer geben: Das ganze läuft schon recht gut, nur leider ist noch ein kleiner Bug in der Implementierung der HYBI10-En/Dekodierung der den Server ab und zu zum Abstürzen bringt. Das ist natürlich schlecht und muss deshalb erst noch behoben werden! &#8212; [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2012/01/c-lua-websocketserver/' addthis:title='C++ &#38; Lua WebSocketServer ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>
<strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2011/12/managers-life-update/' rel='bookmark' title='Manager&#8217;s Life Update'>Manager&#8217;s Life Update</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Hi,</p>
<p>ich möchte eigentlich nur ein kurzes Update zu meinem Projekt <a href="http://websocketserver.de" title="WebSocketServer">C++ &#038; Lua WebSocketServer</a> geben: Das ganze läuft schon recht gut, nur leider ist noch ein kleiner Bug in der Implementierung der HYBI10-En/Dekodierung der den Server ab und zu zum Abstürzen bringt. Das ist natürlich schlecht und muss deshalb erst noch behoben werden!</p>
<p>&#8212;</p>
<p>Just a small update on the <a href="http://websocketserver.de" title="WebSocketServer">C++ &#038; Lua WebSocketServer</a>: Due to a bug in my hybi10-implementation the release will be delayed until I fix that problem &#8211; stay tuned!</p>
<p>&#8212;</p>
<p>-agrafix</p>
<img src="http://blog.agrafix.net/?ak_action=api_record_view&id=529&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2012/01/c-lua-websocketserver/' addthis:title='C++ &amp; Lua WebSocketServer ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><p><strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2011/12/managers-life-update/' rel='bookmark' title='Manager&#8217;s Life Update'>Manager&#8217;s Life Update</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.agrafix.net/2012/01/c-lua-websocketserver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keep your scope &#8211; jQuery.proxy</title>
		<link>http://blog.agrafix.net/2012/01/keep-your-scope-jquery-proxy/</link>
		<comments>http://blog.agrafix.net/2012/01/keep-your-scope-jquery-proxy/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 09:16:07 +0000</pubDate>
		<dc:creator>agrafix</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[scope]]></category>

		<guid isPermaLink="false">http://blog.agrafix.net/?p=520</guid>
		<description><![CDATA[Wer kennt das nicht, man arbeitet mit jQuery und einem click-Event, und &#8220;verliert&#8221; dadurch bei Objekten das &#8220;this&#8221;. Kleines Beispiel: 1 2 3 4 5 6 7 8 9 10 11 function MyObject&#40;&#41; &#123; this.hello = &#34;Hello&#34;; &#125; &#160; MyObject.prototype.clickHandler = function&#40;event&#41; &#123; alert&#40;this.hello&#41;; &#125;; &#160; var obj = new MyObject&#40;&#41;; &#160; $&#40;'#clickMe'&#41;.click&#40;obj.clickHandler&#41;; Wenn man [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2012/01/keep-your-scope-jquery-proxy/' addthis:title='Keep your scope &#8211; jQuery.proxy ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>
<strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2011/10/javascript-timers-mit-jquery/' rel='bookmark' title='JavaScript Timers mit jQuery'>JavaScript Timers mit jQuery</a></li>
<li><a href='http://blog.agrafix.net/2011/02/jquery-in-greasemonkey/' rel='bookmark' title='jQuery in Greasemonkey'>jQuery in Greasemonkey</a></li>
<li><a href='http://blog.agrafix.net/2011/10/ajax-und-der-vor-und-zuruck-button/' rel='bookmark' title='AJAX und der &#8220;Vor&#8221; und &#8220;Zurück&#8221; Button'>AJAX und der &#8220;Vor&#8221; und &#8220;Zurück&#8221; Button</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Wer kennt das nicht, man arbeitet mit<a href="http://jquery.com/" target="_blank" title="jQuery JavaScript Framework"> jQuery </a>und einem click-Event, und &#8220;verliert&#8221; dadurch bei Objekten das &#8220;this&#8221;. Kleines Beispiel:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> MyObject<span style="color: #009900;">&#40;</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;">hello</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Hello&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
MyObject.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">clickHandler</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hello</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MyObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#clickMe'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">clickHandler</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Wenn man jetzt auf ein gedachtes Element mit der ID &#8220;clickMe&#8221; klickt, dann erscheint eine Fehlermeldung in JavaScript bzw. eine Alert-Box mit dem Inhalt &#8220;undefined&#8221; &#8211; obwohl <em>this.hello</em> eigentlich doch <em>&#8220;Hello&#8221;</em> sein sollte? Bei den<a href="http://jquery.com/" target="_blank" title="jQuery JavaScript Framework"> jQuery </a>Event-Handlern &#8220;verliert&#8221; die Funktion ihr aktuelles Scope und das <em>this</em> zeigt dann auf den Sender des Events, in diesem Fall auf das Element mit der ID &#8220;clickMe&#8221;. Das ist zwar oft gewünscht, da man dieses nun zB mit <em>$(this).text(&#8216;Ich wurde geklicked&#8217;);</em> weiterverarbeiten kann, ist aber in meinem Beispiel oben sehr ungünstig. Deshalb bietet die jQuery-Library eine Funktion <em>jQuery.proxy</em>, welche eine Proxy-Funktion generiert mit der wir das Scope einer Funktion selbst beeinflussen können. Ersetzen wir Zeile 11 also mit folgendem:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>11
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#clickMe'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">proxy</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">clickHandler</span><span style="color: #339933;">,</span> obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Nun zeigt das <em>this</em> wärend unserem clickHandler-Aufruf wieder auf das eigentliche Objekt und eine Alert-Box mit dem Inhalt &#8220;Hello&#8221; erscheint. Möchte man nun trotzdem auf das auslösende Element für das Event zugreifen, schafft</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> calledBy <span style="color: #339933;">=</span> event.<span style="color: #660066;">target</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// beispiel</span>
$<span style="color: #009900;">&#40;</span>calledBy<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Link geklickt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>abhilfe! Natürlich kann man auch seine eigene Proxy-Funktion mit direct-calling bauen, was aber den Code schon deutlich unübersichtlicher macht, allerdings viel mehr Flexibilität bietet (wieder Bezug auf 1. Beispielcode):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#clickMe'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		o.<span style="color: #660066;">clickHandler</span><span style="color: #009900;">&#40;</span>event<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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Es lohnt sich also immer auch mal unter &#8220;Misc&#8221; bzw. &#8220;Utilities&#8221; in der <a href="http://api.jquery.com/jQuery.proxy/" title="jQuery.proxy">Dokumentation einer Library</a> nachsehen <img src='http://blog.agrafix.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<img src="http://blog.agrafix.net/?ak_action=api_record_view&id=520&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2012/01/keep-your-scope-jquery-proxy/' addthis:title='Keep your scope &#8211; jQuery.proxy ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><p><strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2011/10/javascript-timers-mit-jquery/' rel='bookmark' title='JavaScript Timers mit jQuery'>JavaScript Timers mit jQuery</a></li>
<li><a href='http://blog.agrafix.net/2011/02/jquery-in-greasemonkey/' rel='bookmark' title='jQuery in Greasemonkey'>jQuery in Greasemonkey</a></li>
<li><a href='http://blog.agrafix.net/2011/10/ajax-und-der-vor-und-zuruck-button/' rel='bookmark' title='AJAX und der &#8220;Vor&#8221; und &#8220;Zurück&#8221; Button'>AJAX und der &#8220;Vor&#8221; und &#8220;Zurück&#8221; Button</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.agrafix.net/2012/01/keep-your-scope-jquery-proxy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manager&#8217;s Life Update</title>
		<link>http://blog.agrafix.net/2011/12/managers-life-update/</link>
		<comments>http://blog.agrafix.net/2011/12/managers-life-update/#comments</comments>
		<pubDate>Sat, 17 Dec 2011 04:15:41 +0000</pubDate>
		<dc:creator>agrafix</dc:creator>
				<category><![CDATA[Projekte]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[managerslife]]></category>
		<category><![CDATA[poker]]></category>

		<guid isPermaLink="false">http://blog.agrafix.net/?p=514</guid>
		<description><![CDATA[Gerade habe ich ein neues Manager&#8217;s Life Update eingespielt. Es enthält das versprochene Casino mit dem Poker Multiplayer! Das Poker-Spiel ist komplett in HTML5+Canvas, JavaScript und PHP realisiert und sollte auf jedem HTML5-Endgerät funktionieren. Außerdem habe ich noch viele kleinere Bugs behoben und ein paar Änderungen an der Sprite-Engine vorgenommen. Viel Spaß beim Pokern! Ähnliche [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2011/12/managers-life-update/' addthis:title='Manager&#8217;s Life Update ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>
<strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2011/11/managers-life-v2/' rel='bookmark' title='Manager&#8217;s Life V2'>Manager&#8217;s Life V2</a></li>
<li><a href='http://blog.agrafix.net/2011/01/visitor-stats-de-update/' rel='bookmark' title='Visitor-Stats.de Update'>Visitor-Stats.de Update</a></li>
<li><a href='http://blog.agrafix.net/2011/11/sprechblasen-mit-html5-canvas-und-javascript/' rel='bookmark' title='Sprechblasen mit HTML5, Canvas und JavaScript'>Sprechblasen mit HTML5, Canvas und JavaScript</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Gerade habe ich ein neues <a href="http://www.managerslife.de">Manager&#8217;s Life</a> Update eingespielt. Es enthält das versprochene Casino mit dem Poker Multiplayer!</p>
<p>Das Poker-Spiel ist komplett in HTML5+Canvas, JavaScript und PHP realisiert und sollte auf jedem HTML5-Endgerät funktionieren.</p>
<p>Außerdem habe ich noch viele kleinere Bugs behoben und ein paar Änderungen an der Sprite-Engine vorgenommen.</p>
<p>Viel Spaß beim Pokern! <img src='http://blog.agrafix.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<img src="http://blog.agrafix.net/?ak_action=api_record_view&id=514&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2011/12/managers-life-update/' addthis:title='Manager&#8217;s Life Update ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><p><strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2011/11/managers-life-v2/' rel='bookmark' title='Manager&#8217;s Life V2'>Manager&#8217;s Life V2</a></li>
<li><a href='http://blog.agrafix.net/2011/01/visitor-stats-de-update/' rel='bookmark' title='Visitor-Stats.de Update'>Visitor-Stats.de Update</a></li>
<li><a href='http://blog.agrafix.net/2011/11/sprechblasen-mit-html5-canvas-und-javascript/' rel='bookmark' title='Sprechblasen mit HTML5, Canvas und JavaScript'>Sprechblasen mit HTML5, Canvas und JavaScript</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.agrafix.net/2011/12/managers-life-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Poker Abstraktion</title>
		<link>http://blog.agrafix.net/2011/12/poker-abstraktion/</link>
		<comments>http://blog.agrafix.net/2011/12/poker-abstraktion/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 09:46:35 +0000</pubDate>
		<dc:creator>agrafix</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[poker]]></category>

		<guid isPermaLink="false">http://blog.agrafix.net/?p=501</guid>
		<description><![CDATA[Wie bereits angekündigt wird die nächste Version von Manager&#8217;s Life ein Casino in die Spielwelt integrieren. Dabei darf natürlich kein Multiplayer-Poker fehlen! Poker mit PHP umzusetzen ist eigentlich eine relativ einfache Sache &#8211; der schwierigere Teil ist das erkennen der Poker-Hände beim Showdown (Flush, Straight, Pair, &#8230;). Natürlich kann man einfach alle möglichen Hände hardcoden [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2011/12/poker-abstraktion/' addthis:title='Poker Abstraktion ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>
<em>Keine ähnlichen Beiträge vorhanden</em>]]></description>
			<content:encoded><![CDATA[<p>Wie bereits angekündigt wird die nächste Version von <a href="http://managerslife.de" title="Manager's Life" target="_blank">Manager&#8217;s Life</a> ein Casino in die Spielwelt integrieren. Dabei darf natürlich kein Multiplayer-Poker fehlen! </p>
<p>Poker mit PHP umzusetzen ist eigentlich eine relativ einfache Sache &#8211; der schwierigere Teil ist das erkennen der <a href="http://de.wikipedia.org/wiki/Hand_%28Poker%29" title="Hand (Poker)" target="_blank">Poker-Hände</a> beim Showdown (Flush, Straight, Pair, &#8230;). Natürlich kann man einfach alle möglichen Hände hardcoden und dann einfach eine nach der anderen Abarbeiten (siehe zB <a href="http://www.phpclasses.org/package/3149-PHP-Shuffle-and-evaluate-a-Poker-deck-of-cards.html" title="phppokerengine" target="_blank">phppokerengine</a>), das ist aber nicht besonderst elegant, performant oder erweiterbar. Außerdem ist es sehr unübersichtlich und fehleranfällig. Deshalb habe ich mir gedacht, man könnte alle möglichen Poker-Hände mit einer Abstraktions-Sprache definieren und diese dann mit entsprechendem Parser zu behandeln. </p>
<p>Zuerst zu meiner Sprache, die ich wie folgt definiert habe:</p>
<ul>
<li>Gleiche Zahlen stellen gleiche Karten-Werte dar</li>
<li>Gleiche Buchstaben stellen gleiche Farben dar</li>
<li>Ein Fragezeichen steht für eine beliebige Karte</li>
<li>Ein <strong>&gt;</strong> sagt, dass die nächste Karte direkt auf die vorherige folgen muss.</li>
<li>Ein <strong>[</strong> sagt, dass das nächste Zeichen kein Symbol, sondern kein expliziter Kartenwert ist</li>
<li><strong>{<em>x</em>}</strong> nach einem Symbol gibt an, wie oft es wiederholt wird</li>
</ul>
<p></p>
<p>Um das ganze zu konkretisieren habe ich nun alle Poker-Hände in meine Sprache übersetzt:</p>
<p><code>// pair (zB &hearts;A - &diams;A)<br />
1{2}</p>
<p>// two pair (zB &clubs;A - &diams;A - &spades;3 - &clubs;3)<br />
1{2}2{2}</p>
<p>// three of a kind (zB &clubs;A - &diams;A - &spades;A)<br />
1{3}</p>
<p>// straight (zB &clubs;K - &diams;Q - &spades;J - &clubs;10 - &clubs;9)<br />
?>?>?>?>?</p>
<p>// flush (zB &clubs;2 - &clubs;Q - &clubs;9 - &clubs;10 - &clubs;9)<br />
a{5}</p>
<p>// full house  (zB &clubs;K - &diams;K - &spades;Q - &clubs;Q - &clubs;Q)<br />
1{3}2{2}</p>
<p>// four of a kind  (zB &clubs;K - &diams;K - &spades;K - &clubs;K)<br />
1{4}</p>
<p>// straight flush  (zB &clubs;K - &clubs;Q - &clubs;J - &clubs;10 - &clubs;9)<br />
a>a>a>a>a</p>
<p>// royal flush (zB &clubs;A - &clubs;K - &clubs;Q - &clubs;J - &clubs;10)<br />
[a>a>a>a>a</code></p>
<p>Das Parsen und Verarbeiten ist zwar etwas trickreicher, aber machbar. Ich habe einen Quick-n-Dirty PHP-Prototypen gebaut &#8211; er funktioniert, ist allerdings nicht besonderst sauber geschrieben. Im Vergleich zu der Methode &#8220;alle-Möglichkeiten-hardcoren&#8221; entsteht übrigens noch ein weiterer Vorteil: Meine Klasse ist 7kB groß, die oben verlinkte 0,5 MB&#8230;</p>
<p>Die Verwendung der Klasse erfolgt so:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// straight flush detector</span>
<span style="color: #000088;">$parser</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PokerParser<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a&gt;a&gt;a&gt;a&gt;a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$hand</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'color'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'spades'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'card'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'k'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'color'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'spades'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'card'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'color'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'spades'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'card'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'j'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'color'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'spades'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'card'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'10'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'color'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'spades'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'card'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'9'</span><span style="color: #009900;">&#41;</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;">$parser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hand</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Deine Hand enthält ein Straight Flush!&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Kein Straight Flush vorhanden.&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Die Ausgabe sollte &#8220;Deine Hand enthält ein Straight Flush!&#8221; sein.</p>
<p>Die Klasse findet sich dort: <a href='http://blog.agrafix.net/wp-content/uploads/2011/12/PokerParser.class.php_.txt'>PokerParser.class.php</a></p>
<img src="http://blog.agrafix.net/?ak_action=api_record_view&id=501&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2011/12/poker-abstraktion/' addthis:title='Poker Abstraktion ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><p><em>Keine ähnlichen Beiträge vorhanden</em></p>]]></content:encoded>
			<wfw:commentRss>http://blog.agrafix.net/2011/12/poker-abstraktion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RedBean PHP &#8211; Top oder Flop?</title>
		<link>http://blog.agrafix.net/2011/12/redbean-php-top-oder-flop/</link>
		<comments>http://blog.agrafix.net/2011/12/redbean-php-top-oder-flop/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 13:12:58 +0000</pubDate>
		<dc:creator>agrafix</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[redbean]]></category>

		<guid isPermaLink="false">http://blog.agrafix.net/?p=497</guid>
		<description><![CDATA[Eine Datenbank-Abstraktion mit ORM kann sehr praktisch sein und viel Arbeit sparen, vorallem wenn sie so funktioniert wie RedBean-PHP. Nun, wie funktioniert RedBean-PHP? Eigentlich relativ simpel. Anderst als bei sehr bekannten PHP-ORM-Libraries wie Doctrine oder Propel muss sich der Programmierer theoretisch gar nicht um das Layout der Datenbank kümmern. Er muss also keine Felder oder [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2011/12/redbean-php-top-oder-flop/' addthis:title='RedBean PHP &#8211; Top oder Flop? ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>
<em>Keine ähnlichen Beiträge vorhanden</em>]]></description>
			<content:encoded><![CDATA[<p>Eine Datenbank-Abstraktion mit <a href="http://de.wikipedia.org/wiki/Objektrelationale_Abbildung" title="ORM" target="_blank">ORM</a> kann sehr praktisch sein und viel Arbeit sparen, vorallem wenn sie so funktioniert wie <a href="http://redbeanphp.com/" title="RedBean PHP" target="_blank">RedBean-PHP</a>.<br />
Nun, wie funktioniert RedBean-PHP? Eigentlich relativ simpel. Anderst als bei sehr bekannten PHP-ORM-Libraries wie Doctrine oder Propel muss sich der Programmierer theoretisch gar nicht um das Layout der Datenbank kümmern. Er muss also keine Felder oder Relationen vor dem eigentlichen Programmieren definieren, sondern kann sie einfach verwenden, und RedBean-PHP kümmert sich um den Rest. Klingt ziemlich cool, oder? Hier mal ein Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// RedBean-PHP besteht aus einer einzigen Datei</span>
<span style="color: #b1b100;">require</span> <span style="color: #0000ff;">&quot;rb.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Zur Datenbank verbinden:</span>
<span style="color: #666666; font-style: italic;">// derzeit wird SQLite, MySQL und PostgreSQL unterstützt</span>
R<span style="color: #339933;">::</span><span style="color: #004000;">setup</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysql:host=localhost;dbname=redbeantest'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'root'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ein haus erstellen</span>
<span style="color: #000088;">$house</span> <span style="color: #339933;">=</span> R<span style="color: #339933;">::</span><span style="color: #004000;">dispense</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'house'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$house</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">color</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'blue'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$house</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">size</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">34</span><span style="color: #339933;">;</span>
R<span style="color: #339933;">::</span><span style="color: #004000;">store</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$house</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// in der db speichern</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// einen besitzer erstellen</span>
<span style="color: #000088;">$owner</span> <span style="color: #339933;">=</span> R<span style="color: #339933;">::</span><span style="color: #004000;">dispense</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'owner'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$owner</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Alex'</span><span style="color: #339933;">;</span>
R<span style="color: #339933;">::</span><span style="color: #004000;">store</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$owner</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// in der db speichern</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// das Haus gehört dem Besitzer</span>
<span style="color: #666666; font-style: italic;">// Many-to-Many Relation, da jeder Besitzer </span>
<span style="color: #666666; font-style: italic;">// viele Häuser haben kann und ein Haus </span>
<span style="color: #666666; font-style: italic;">// mehrere Besitzer</span>
R<span style="color: #339933;">::</span><span style="color: #004000;">associate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$owner</span><span style="color: #339933;">,</span> <span style="color: #000088;">$house</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// eine stadt erstellen</span>
<span style="color: #000088;">$city</span> <span style="color: #339933;">=</span> R<span style="color: #339933;">::</span><span style="color: #004000;">dispense</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'city'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$city</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Zauberstadt'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// eine One-to-Many Relation, da </span>
<span style="color: #666666; font-style: italic;">// jedes Haus nur zu einer Stadt </span>
<span style="color: #666666; font-style: italic;">// gehört</span>
<span style="color: #000088;">$city</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ownHouse</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$house</span><span style="color: #339933;">;</span> 
&nbsp;
R<span style="color: #339933;">::</span><span style="color: #004000;">store</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$city</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// speichern</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// jetzt laden wir mal ein paar sachen aus der DB</span>
<span style="color: #666666; font-style: italic;">// einen besitzer mit dem name Alex finden</span>
<span style="color: #000088;">$person</span> <span style="color: #339933;">=</span> R<span style="color: #339933;">::</span><span style="color: #004000;">findOne</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'owner'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' name = ?'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Alex'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// seine häuser finden</span>
<span style="color: #000088;">$houses</span> <span style="color: #339933;">=</span> R<span style="color: #339933;">::</span><span style="color: #004000;">related</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'house'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$houses</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// häuser dumpen</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// die häuser der stadt ausgeben</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$city</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ownHouse</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$house</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Haus Nummer <span style="color: #006699; font-weight: bold;">$key</span> ist &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$house</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">color</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Das ganze funktioniert, sobald es eine Datenbank &#8220;redbeantest&#8221; gibt. Es müssen keine Tabellen in der Datenbank erstellt werden, noch Forgein-Keys angelegt werden. (Weitere Beispiele und die Dokumentation sind auf der <a href="http://www.redbeanphp.com/" title="RedBean PHP" target="_blank">Webseite</a>.)</p>
<p>Alles wunderbar, allerdings kommt hier schon ein erstes Problem auf! RedBean PHP stellt nämlich alle Forgein-Keys und dessen Relations auf <em>ON UPDATE SET NULL, ON DELETE SET NULL</em>. Dieses Verhalten ist aber nicht immer gewünscht, das heißt hier muss man aufjedenfall noch nach RedBean-PHP aufräumen bevor die Software in Produktion gehen kann. Ein weiteres Problem ist, dass RedBean-PHP den Typ eines Datenbankfelds nur dann optimal wählt, wenn man es mit genügend Test-Daten füttert. Ansonsten ist auch hier ein nachträgliches Aufräumen nötig. Hier kann man sich also die Frage stellen &#8211; möchte ich lieber später meine Datenbank aufräumen müssen, oder sie direkt von Anfang an korrekt designen?</p>
<p>Ein zweites Problem von RedBean-PHP ist Performance. Das Framework macht, vorallem bei Relations, einfach viel zu viele Datenbank-Abfragen, die bei vielen Zugriffen zu hoher Datenbanklast führen können (Abhilfe schafft hier ein Caching System, allerdings ist das nur hilfreich, wenn keine aktuellen Daten benötigt werden). Hier endet man dann also damit einige R::find und R::related durch eigene Queries zu ersetzen. (was übrigens auch prima funktioniert, aber dann nicht mehr im Sinne von ORM ist&#8230;)</p>
<p>Abgesehen davon ist es aber eine super Sache, man kommt aufjedenfall schnell zu Ergebnissen. Ich würde es für alle kleineren PHP-Projekte empfehlen! Für größere Projekte würde ich davon abraten, da man einfach zu schnell den Überblick verliert und man mit dem &#8220;Aufräumen&#8221; schnell mal etwas übersieht was dann zu kritischen Fehlern führen kann.</p>
<img src="http://blog.agrafix.net/?ak_action=api_record_view&id=497&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2011/12/redbean-php-top-oder-flop/' addthis:title='RedBean PHP &#8211; Top oder Flop? ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><p><em>Keine ähnlichen Beiträge vorhanden</em></p>]]></content:encoded>
			<wfw:commentRss>http://blog.agrafix.net/2011/12/redbean-php-top-oder-flop/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>HTML5 WebSocket</title>
		<link>http://blog.agrafix.net/2011/11/html5-websocket/</link>
		<comments>http://blog.agrafix.net/2011/11/html5-websocket/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 11:08:57 +0000</pubDate>
		<dc:creator>agrafix</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[TCP]]></category>
		<category><![CDATA[WebSocket]]></category>

		<guid isPermaLink="false">http://blog.agrafix.net/?p=482</guid>
		<description><![CDATA[HTML5 unterstützt jetzt das Aufbauen von &#8220;WebSocket&#8221;-Verbindungen. Eine WebSocket Verbindung basiert ähnlich wie HTTP auf dem TCP Protokoll, baut aber eine &#8220;dauerhafte&#8221; Verbindung auf. Die Vorteile gegenüber HTTP ist, dass eine Aktion des Servers keine vorhergehende Anfrage des Clients mehr erfordert, wodurch der Server neue Informationen fasst in Echtzeit ausliefern kann. Dadurch kann man eine [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2011/11/html5-websocket/' addthis:title='HTML5 WebSocket ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>
<strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2011/11/sprechblasen-mit-html5-canvas-und-javascript/' rel='bookmark' title='Sprechblasen mit HTML5, Canvas und JavaScript'>Sprechblasen mit HTML5, Canvas und JavaScript</a></li>
<li><a href='http://blog.agrafix.net/2011/10/javascript-und-geolocation/' rel='bookmark' title='JavaScript und Geolocation'>JavaScript und Geolocation</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>HTML5 unterstützt jetzt das Aufbauen von &#8220;WebSocket&#8221;-Verbindungen. Eine WebSocket Verbindung basiert ähnlich wie HTTP auf dem TCP Protokoll, baut aber eine &#8220;dauerhafte&#8221; Verbindung auf. Die Vorteile gegenüber HTTP ist, dass eine Aktion des Servers keine vorhergehende Anfrage des Clients mehr erfordert, wodurch der Server neue Informationen fasst in Echtzeit ausliefern kann. Dadurch kann man eine Menge Traffic und viele unnötige HTTP-Requests sparen, die etwa ein AJAX-JavaScript sonst schicken würde um neue Informationen zu erhalten.<br />
Technisch gesehen baut bei einer WebSocket-Verbindung der Browser mit dem Server eine Verbindung auf, die nach einen Handshake dann bestehen bleibt und über die dann Binär- und Textdaten geschickt werden können.</p>
<p>Leider hat das Protokoll im Handshake einige Sicherheitsprobleme (gehabt?) und sich deshalb ständig verändert. Dieser Artikel bezieht sich also auf die Aktuellste Version (<a href="https://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17" title="17" target="_blank">17</a>). Diese wird derzeit von der aktuellsten Chrome Version und anderen Webkit Browsern unterstützt, in Firefox und Opera ist WebSocket wegen der Sicherheitsprobleme derzeit deaktiviert.</p>
<p>Um HTML5 WebSocket zu verwenden ist zunächst ein WebSocket-Server nötig. Ich habe hier nach einer PHP-Variante gesucht und habe dann nach einer Weile folgenden gefunden: <a href="https://github.com/lemmingzshadow/php-websocket" title="PHP-WebSocket" target="_blank">php-websocket</a>. Diesen sollte man sich laden und dann kann man ihn per</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">php <span style="color: #660033;">-q</span> php-websocket<span style="color: #000000; font-weight: bold;">/</span>server<span style="color: #000000; font-weight: bold;">/</span>server.php</pre></div></div>

<p> starten. Jetzt ist ein WebSocket-Server auf &#8220;localhost:8000&#8243; zu erreichen. Dort sind derzeit zwei Module geladen, einmal das Modul &#8220;echo&#8221; und einmal das Modul &#8220;chat&#8221;. (Ich werde mich hier nicht mit der Entwicklung eines WebSocket-Server weiter befassen) Das &#8220;echo&#8221; Modul nimmt einfach Anfragen entgegen und schickt diese dann an alle Verbundenen Sockets weiter.</p>
<p>Nun kümmern wir uns um die HTML5-Seite von WebSocket, dem User-Frontend. Dafür bauen wir uns erstmal eine einfache HTML5 Seite:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;UTF-8&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>HTML5 WebSocket Test<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!-- jQuery 1.6.4 zur DOM-Manipulation --&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery-1.6.4.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!-- meine eigene Wrapper-Library für WebSocket Verbindungen, siehe unten --&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;websocketlib.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
// hier wird dann das WebSocket-Script reinkommen, siehe unten
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;float:right;width:40%;&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Log<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;logbox&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Chatbox<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;chatbox&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Say something<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'chat'</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text'</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">''</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'message'</span> <span style="color: #66cc66;">/</span>&gt;</span>
	 <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'submit'</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Sagen'</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Für die einfache Verwendung des WebSockets habe ich mir eine kleine Wrapper-Library (<b>websocketlib.js</b>) programmiert:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * Small wrapper library for HTML5 WebSocket
 * 
 * @author Alexander Thiemann &lt;mail@agrafix.net&gt;
 * @version 1.0
 * 
 * @param string WebSocket host, eg. ws://localhost:8010/echo
 * @param function Function to be called when socket is connected
 * @param function Function to be called when socket recieves data, takes 1 argument message
 * @param function Function to be called for logging purposes, takes 1 argument log-message
 * 
 * @see http://blog.agrafix.net/2011/11/html5-websocket/
 */</span>
<span style="color: #003366; font-weight: bold;">var</span> WebSocketLib <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	init<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>host<span style="color: #339933;">,</span> onConnect<span style="color: #339933;">,</span> onRecv<span style="color: #339933;">,</span> onLog<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;">socket</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> WebSocket<span style="color: #009900;">&#40;</span>host<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onLog</span> <span style="color: #339933;">=</span> onLog<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onRecv</span> <span style="color: #339933;">=</span> onRecv<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onConnect</span> <span style="color: #339933;">=</span> onConnect<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onLog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Initializing Socket. State: '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">socket</span>.<span style="color: #660066;">readyState</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">socket</span>.<span style="color: #660066;">onopen</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._onOpen<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">socket</span>.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._onMessage<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">socket</span>.<span style="color: #660066;">onclose</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._onClose<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	send<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		WebSocketLib.<span style="color: #660066;">socket</span>.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	_onClose<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		WebSocketLib.<span style="color: #660066;">onLog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Socket Closed.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	_onMessage<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		WebSocketLib.<span style="color: #660066;">onLog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Recieved new Message from Server: '</span> <span style="color: #339933;">+</span> msg.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		WebSocketLib.<span style="color: #660066;">onRecv</span><span style="color: #009900;">&#40;</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>
&nbsp;
	_onOpen<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		WebSocketLib.<span style="color: #660066;">onLog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Socket is Ready. State: '</span> <span style="color: #339933;">+</span> WebSocketLib.<span style="color: #660066;">socket</span>.<span style="color: #660066;">readyState</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>WebSocketLib.<span style="color: #660066;">socket</span>.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			WebSocketLib.<span style="color: #660066;">onConnect</span><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: #009900;">&#125;</span></pre></div></div>

<p>Ich denke dort ist alles relativ selbsterklärend. Das WebSocket ist wirklich sehr leicht zu verwenden! Um diese Wrapper-Library zu verwenden, bauen wir in obiges HTML-Grundgerüst bei &#8220;<em>// hier wird dann das WebSocket-Script reinkommen, siehe unten</em>&#8221; folgendes ein:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	WebSocketLib.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ws://localhost:8010/echo'</span><span style="color: #339933;">,</span> 
	<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		sockReady<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: #339933;">,</span>
	<span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#chatbox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;p&gt;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</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: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#logbox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;p&gt;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> sockReady<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#chat'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">var</span> input <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[name=&quot;message&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			WebSocketLib.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>input.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			input.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Zuerst warten wir, bis unser HTML-Dokument im Browser aufgebaut ist. Dann geben wir in Zeile 2 an, wo unser WebSocket-Server läuft. Die Funktion in Zeile 4 wird aufgerufen, sobald das WebSocket verbunden ist. Die Funktion Zeile 6-8 wird aufgerufen, wenn eine Nachricht zurückgegeben wird. Die Funktion Zeile 9-11 wird bei Log-Ereignissen (Hauptsächlich zum Debuggen) aufgerufen. Mit WebSocketLib.send(&#8220;Nachricht&#8221;) in Zeile 19 schicken wir eine Nachricht an den Server.</p>
<p>Wenn nun alles vorbereitet ist, der WebSocket-Server gestartet ist (siehe oben), können wir einem Browser, der WebSocket unterstützt (siehe oben), unsere HTML-Seite aufrufen. Die Log-Ausgabe im HTML-Fenster sieht wie folgt aus:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Initializing Socket. State: 0
Socket is Ready. State: 1</pre></div></div>

<p>Im Consolenfenster des PHP-Server sehen wir:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">2011-11-25 11:26:47 [info] [client 127.0.0.1:30601] Connected
2011-11-25 11:26:47 [info] [client 127.0.0.1:30601] Performing handshake
2011-11-25 11:26:47 [info] [client 127.0.0.1:30601] Handshake sent</pre></div></div>

<p>Die Verbindung ist nun also aufgebaut. Jetzt sollten wir den &#8220;echo&#8221;-Server testen. Wir tippen etwas in das Textfeld und schicken es ab. Der Log sagt:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Recieved new Message from Server: test</pre></div></div>

<p>Und unter &#8220;Chat&#8221; steht jetzt &#8220;test&#8221;. Hat soweit also funktioniert! Jetzt können wir ein neues Browserfenster aufmachen, wieder den Verbindungsvorgang abwarten und dann dort eine Nachricht verschicken. Diese erscheint dann in allen verbundenen Client-Fenstern.</p>
<p>Eigentlich ziemlich einfach zu verwenden, und es öffnet viele neue Möglichkeiten vorallem in Kombination mit Canvas für HTML5-Multiplayer-Games&#8230; hoffen wir also, dass es bald von allen Browser voll unterstützt wird! <img src='http://blog.agrafix.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<img src="http://blog.agrafix.net/?ak_action=api_record_view&id=482&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2011/11/html5-websocket/' addthis:title='HTML5 WebSocket ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><p><strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2011/11/sprechblasen-mit-html5-canvas-und-javascript/' rel='bookmark' title='Sprechblasen mit HTML5, Canvas und JavaScript'>Sprechblasen mit HTML5, Canvas und JavaScript</a></li>
<li><a href='http://blog.agrafix.net/2011/10/javascript-und-geolocation/' rel='bookmark' title='JavaScript und Geolocation'>JavaScript und Geolocation</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.agrafix.net/2011/11/html5-websocket/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sprechblasen mit HTML5, Canvas und JavaScript</title>
		<link>http://blog.agrafix.net/2011/11/sprechblasen-mit-html5-canvas-und-javascript/</link>
		<comments>http://blog.agrafix.net/2011/11/sprechblasen-mit-html5-canvas-und-javascript/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 04:42:54 +0000</pubDate>
		<dc:creator>agrafix</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[bubble]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[speech]]></category>
		<category><![CDATA[speechbubble]]></category>
		<category><![CDATA[sprechblase]]></category>

		<guid isPermaLink="false">http://blog.agrafix.net/?p=478</guid>
		<description><![CDATA[Mit HTML5, Canvas und JavaScript kann man mittlerweile eine Menge schöner Grafiken direkt im Browser rendern. Da ich neulich eine flexible Sprechblase benötigt habe, hier der Code dafür: /** * Draw a speech bubble on an HTML5-Canvas * * @author Alexander Thiemann &#60;mail@agrafix.net&#62; * @license CC BY-SA 2.0 * * @param ctx Canvas 2d context [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2011/11/sprechblasen-mit-html5-canvas-und-javascript/' addthis:title='Sprechblasen mit HTML5, Canvas und JavaScript ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>
<strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2011/11/html5-websocket/' rel='bookmark' title='HTML5 WebSocket'>HTML5 WebSocket</a></li>
<li><a href='http://blog.agrafix.net/2009/08/in_array-fur-javascript/' rel='bookmark' title='in_array() für Javascript'>in_array() für Javascript</a></li>
<li><a href='http://blog.agrafix.net/2011/10/javascript-und-geolocation/' rel='bookmark' title='JavaScript und Geolocation'>JavaScript und Geolocation</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Mit HTML5, Canvas und JavaScript kann man mittlerweile eine Menge schöner Grafiken direkt im Browser rendern. Da ich neulich eine flexible Sprechblase benötigt habe, hier der Code dafür:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * Draw a speech bubble on an HTML5-Canvas
 *
 * @author Alexander Thiemann &lt;mail@agrafix.net&gt;
 * @license CC BY-SA 2.0
 * 
 * @param ctx Canvas 2d context
 * @param text Text for the speech bubble
 * @param x bottom left x-coordinate of speech bubble
 * @param y bottom left y-coordinate of speech bubble
 *
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> speechBubble<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> text<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> messure <span style="color: #339933;">=</span> ctx.<span style="color: #660066;">measureText</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> w <span style="color: #339933;">=</span> messure.<span style="color: #660066;">width</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> h <span style="color: #339933;">=</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">;</span>
&nbsp;
	ctx.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ctx.<span style="color: #660066;">strokeStyle</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;black&quot;</span><span style="color: #339933;">;</span>
	ctx.<span style="color: #660066;">lineWidth</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">;</span>
	ctx.<span style="color: #660066;">fillStyle</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;rgba(255, 255, 255, 0.8)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	ctx.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>w<span style="color: #339933;">*</span><span style="color: #CC0000;">0.2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>w<span style="color: #339933;">*</span><span style="color: #CC0000;">0.2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y<span style="color: #339933;">+</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>w<span style="color: #339933;">*</span><span style="color: #CC0000;">0.3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> w<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	ctx.<span style="color: #660066;">quadraticCurveTo</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>w<span style="color: #339933;">*</span><span style="color: #CC0000;">1.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> x <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>w<span style="color: #339933;">*</span><span style="color: #CC0000;">1.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>h<span style="color: #339933;">*</span><span style="color: #CC0000;">0.2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// corner: right-bottom</span>
&nbsp;
	ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>w<span style="color: #339933;">*</span><span style="color: #CC0000;">1.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>h<span style="color: #339933;">*</span><span style="color: #CC0000;">0.8</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// right</span>
&nbsp;
	ctx.<span style="color: #660066;">quadraticCurveTo</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>w<span style="color: #339933;">*</span><span style="color: #CC0000;">1.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y<span style="color: #339933;">-</span>h<span style="color: #339933;">,</span> x <span style="color: #339933;">+</span> w<span style="color: #339933;">,</span> y<span style="color: #339933;">-</span>h<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// corner: right-top</span>
&nbsp;
	ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">-</span>h<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// top</span>
&nbsp;
	ctx.<span style="color: #660066;">quadraticCurveTo</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>w<span style="color: #339933;">*</span><span style="color: #CC0000;">0.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y<span style="color: #339933;">-</span>h<span style="color: #339933;">,</span> x <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>w<span style="color: #339933;">*</span><span style="color: #CC0000;">0.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>h<span style="color: #339933;">*</span><span style="color: #CC0000;">0.8</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// corner: left-top</span>
&nbsp;
	ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>w<span style="color: #339933;">*</span><span style="color: #CC0000;">0.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>h<span style="color: #339933;">*</span><span style="color: #CC0000;">0.2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// left</span>
&nbsp;
	ctx.<span style="color: #660066;">quadraticCurveTo</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>w<span style="color: #339933;">*</span><span style="color: #CC0000;">0.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// corner: left-bottom</span>
&nbsp;
	ctx.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ctx.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ctx.<span style="color: #660066;">closePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	ctx.<span style="color: #660066;">textAlign</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'left'</span><span style="color: #339933;">;</span>
	ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#000'</span><span style="color: #339933;">;</span>
	ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span>text<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #339933;">-</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Verwendung:<br />
<em>HTML</em></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;canvas id=&quot;bubble&quot;&gt;&lt;/canvas&gt;</pre></div></div>

<p><em>JavaScript</em></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> ctx <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;bubble&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
speechBubble<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Das hier ist ein Test!&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Das Resultat:<br />
<a href="http://blog.agrafix.net/wp-content/uploads/2011/11/canvasSpeechBubble.png" rel="lightbox[478]" title="HTML5 Canvas Sprechblase"><img src="http://blog.agrafix.net/wp-content/uploads/2011/11/canvasSpeechBubble.png" alt="Realisiert mit HTML5, Canvas und JavaScript" title="HTML5 Canvas Sprechblase" width="125" height="67" class="alignnone size-full wp-image-479" /></a></p>
<p>Ziemlich schick, oder? <img src='http://blog.agrafix.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<img src="http://blog.agrafix.net/?ak_action=api_record_view&id=478&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.agrafix.net/2011/11/sprechblasen-mit-html5-canvas-und-javascript/' addthis:title='Sprechblasen mit HTML5, Canvas und JavaScript ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><p><strong>Ähnliche Beiträge:</strong><ol>
<li><a href='http://blog.agrafix.net/2011/11/html5-websocket/' rel='bookmark' title='HTML5 WebSocket'>HTML5 WebSocket</a></li>
<li><a href='http://blog.agrafix.net/2009/08/in_array-fur-javascript/' rel='bookmark' title='in_array() für Javascript'>in_array() für Javascript</a></li>
<li><a href='http://blog.agrafix.net/2011/10/javascript-und-geolocation/' rel='bookmark' title='JavaScript und Geolocation'>JavaScript und Geolocation</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.agrafix.net/2011/11/sprechblasen-mit-html5-canvas-und-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

