<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[实践OK]WebSocket的JavaScript例子，结合swoole的PHP扩展及swoole_framework提供的example来编写并摸索备案。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Fri, 22 Aug 2014 09:08:20 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：swoole有一个Websocket示例，我作为swoole的顾问,群里反映cocos2dx下的websocket长连接，语言是不一样，但websocket是一样的，swoole是支持Websocket的，作下简单实践。<br/>WebSocket的JavaScript例子，一个WebSocket的简单Echo例子：<br/>例子代码来自：http://www.websocket.org/echo.html<br/>上面这个链接可以当作一个websocket的工具调试并使用。<br/><br/>一、当然，得下载swoole服务扩展并安装：<br/>git clone https://github.com/swoole/swoole-src.git<br/>You should add &quot;extension=swoole.so&quot; to php.ini<br/>二、动态服务端代码：<br/>cd /data/codesdev/swoole/<br/>git clone https://github.com/matyhtf/swoole_framework.git<br/>/data/codesdev/swoole/swoole_framework/examples/websocket_server.php<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
define(&#039;DEBUG&#039;, &#039;on&#039;);
define(&quot;WEBPATH&quot;, str_replace(&quot;&#92;&#92;&quot;,&quot;/&quot;, __DIR__));
require __DIR__ . &#039;/../libs/lib_config.php&#039;;

class WebSocket extends Swoole&#92;Network&#92;Protocol&#92;WebSocket
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;function onStart($serv, $worker_id = 0)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//$serv-&gt;addTimer(1000);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::onStart($serv, $worker_id);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;/**
&nbsp;&nbsp;&nbsp;&nbsp; * 下线时，通知所有人
&nbsp;&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp;&nbsp;&nbsp;function onClose($serv, $client_id, $from_id)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//将下线消息发送给所有人
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//$this-&gt;log(&quot;onOffline: &quot; . $client_id);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//$this-&gt;broadcast($client_id, &quot;onOffline: &quot; . $client_id);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::onClose($serv, $client_id, $from_id);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;/**
&nbsp;&nbsp;&nbsp;&nbsp; * 接收到消息时
&nbsp;&nbsp;&nbsp;&nbsp; * @see WSProtocol::onMessage()
&nbsp;&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp;&nbsp;&nbsp;function onMessage($client_id, $ws)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;log(&quot;onMessage: &quot;.$client_id.&#039; = &#039;.$ws[&#039;message&#039;]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;send($client_id, &quot;Server: &quot;.$ws[&#039;message&#039;]);
&nbsp;&nbsp;&nbsp;&nbsp;//$this-&gt;broadcast($client_id, $ws[&#039;message&#039;]);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;function broadcast($client_id, $msg)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach ($this-&gt;connections as $clid =&gt; $info)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($client_id != $clid)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;send($clid, $msg);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;function onTimer($serv, $interval)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;timer $interval&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;

//require __DIR__&#039;/phar://swoole.phar&#039;;
Swoole&#92;Config::$debug = true;
Swoole&#92;Error::$echo_html = false;

$AppSvr = new WebSocket();
$AppSvr-&gt;loadSetting(__DIR__.&quot;/swoole.ini&quot;); //加载配置文件
$AppSvr-&gt;setLogger(new &#92;Swoole&#92;Log&#92;EchoLog(true)); //Logger

/**
 * 如果你没有安装swoole扩展，这里还可选择
 * BlockTCP 阻塞的TCP，支持windows平台
 * SelectTCP 使用select做事件循环，支持windows平台
 * EventTCP 使用libevent，需要安装libevent扩展
 */
$enable_ssl = true;
$server = new &#92;Swoole&#92;Network&#92;Server(&#039;0.0.0.0&#039;, 9443, $enable_ssl);
$server-&gt;setProtocol($AppSvr);
//$server-&gt;daemonize(); //作为守护进程
$server-&gt;run(array(
&nbsp;&nbsp;&nbsp;&nbsp;&#039;worker_num&#039; =&gt; 1,
&nbsp;&nbsp;&nbsp;&nbsp;&#039;ssl_key_file&#039; =&gt; __DIR__.&#039;/ssl/ssl.key&#039;,
&nbsp;&nbsp;&nbsp;&nbsp;&#039;ssl_cert_file&#039; =&gt; __DIR__.&#039;/ssl/ssl.crt&#039;,
&nbsp;&nbsp;&nbsp;&nbsp;//&#039;max_request&#039; =&gt; 1000,
&nbsp;&nbsp;&nbsp;&nbsp;//&#039;ipc_mode&#039; =&gt; 2,
&nbsp;&nbsp;&nbsp;&nbsp;//&#039;heartbeat_check_interval&#039; =&gt; 40,
&nbsp;&nbsp;&nbsp;&nbsp;//&#039;heartbeat_idle_time&#039; =&gt; 60,
));

</textarea><br/><br/>上面是swoole_framework提供的代码，我们就直接运行这个服务端的server侦听9443端口：<br/>php websocket_server.php<br/><br/>三、静态Js代码，通过firefox访问下，当然其他支持websocket的浏览器也可以，这块暂没从swoole_framework里摘，在网上摘的js，后面会补上从哪儿摘来的Url地址：<br/>/data/htdocs/tools.jackxiang.com/websocket.html <br/>vi websocket.html<br/><textarea name="code" class="JS" rows="15" cols="100">
&lt;!DOCTYPE html&gt;&nbsp;&nbsp;
&lt;meta charset=&quot;utf-8&quot; /&gt;&nbsp;&nbsp;
&lt;title&gt;WebSocket Test&lt;/title&gt;&nbsp;&nbsp;
&lt;script language=&quot;javascript&quot;type=&quot;text/javascript&quot;&gt;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;var wsUri =&quot;ws://119.10.6.23:9443&quot;; 
&nbsp;&nbsp;&nbsp;&nbsp;var output;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;function init() &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output = document.getElementById(&quot;output&quot;); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//console.log(output);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testWebSocket(); 
&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;function testWebSocket() &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;websocket = new WebSocket(wsUri); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;websocket.onopen = function(evt) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onOpen(evt) 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;websocket.onclose = function(evt) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onClose(evt) 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;websocket.onmessage = function(evt) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onMessage(evt) 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;websocket.onerror = function(evt) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onError(evt) 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;; 
&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;function onOpen(evt) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeToScreen(&quot;CONNECTED&quot;); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doSend(&quot;WebSocket rocks&quot;); 
&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;function onClose(evt) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeToScreen(&quot;DISCONNECTED&quot;); 
&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;function onMessage(evt) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeToScreen(&#039;&lt;span style=&quot;color: blue;&quot;&gt;RESPONSE: &#039;+ evt.data+&#039;&lt;/span&gt;&#039;); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;websocket.close(); 
&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;function onError(evt) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeToScreen(&#039;&lt;span style=&quot;color: red;&quot;&gt;ERROR:&lt;/span&gt; &#039;+ evt.data); 
&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;function doSend(message) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeToScreen(&quot;SENT: &quot; + message);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;websocket.send(message); 
&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;function writeToScreen(message) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var pre = document.createElement(&quot;p&quot;); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre.style.wordWrap = &quot;break-word&quot;; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre.innerHTML = message; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.appendChild(pre); 
&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;window.addEventListener(&quot;load&quot;, init, false);&nbsp;&nbsp;
&lt;/script&gt;&nbsp;&nbsp;
&lt;h2&gt;WebSocket Test&lt;/h2&gt;&nbsp;&nbsp;
&lt;div id=&quot;output&quot;&gt;&lt;/div&gt;&nbsp;&nbsp;
&lt;/html&gt;
</textarea><br/><br/><br/>四、在Firefox或chrome下访问这个支持websocket的Js代码：<br/>WebSocket Test<br/>CONNECTED<br/>SENT: WebSocket rocks<br/>RESPONSE: Server: WebSocket rocks<br/>DISCONNECTED<br/><br/>说明这个php是真支持websocket的，否则不会输入什么就输出什么，得证。<br/><br/>参考：http://www.xyhtml5.com/websocket-javascript-example.html<br/>——后来发现swoole_framework里的example里居然有对这个websocket的php配套的简单测试代码——<br/>后记，其实swoole_framework下提供了和websocket相关的html测试及php客户端测试：<br/>websocket_client.html&nbsp;&nbsp;websocket_client.php&nbsp;&nbsp; websocket_server.php<br/>（1）前端：websocket_client.htm<br/><textarea name="code" class="JS" rows="15" cols="100">
&lt;script&gt;
var wsServer = &#039;wss://127.0.0.1:9443&#039;;
var websocket = new WebSocket(wsServer); 
websocket.onopen = function (evt) &#123; 
&nbsp;&nbsp;console.log(&quot;Connected to WebSocket server.&quot;);
&nbsp;&nbsp;//websocket.send(&quot;swoole ,I am jack...&quot;); //如果打开可以看到服务端回你在console.log里。
&#125;; 

websocket.onclose = function (evt) &#123; 
&nbsp;&nbsp;console.log(&quot;Disconnected&quot;); 
&#125;; 

websocket.onmessage = function (evt) &#123; 
&nbsp;&nbsp;console.log(&#039;Retrieved data from server: &#039; + evt.data); //上面注释打开在firebug里看到：Retrieved data from server: Server: swoole ,I am jack...
&#125;; 

websocket.onerror = function (evt, e) &#123;
&nbsp;&nbsp;console.log(&#039;Error occured: &#039; + evt.data);
&#125;;
&lt;/script&gt;

</textarea><br/>把wss换成ws，wss好像是https的websocket：<br/>//var wsServer = &#039;wss://127.0.0.1:9443&#039;; //IP也换下<br/>var wsServer = &#039;ws://119.10.6.23:9443&#039;;<br/>访问下：http://tools.jackxiang.com/websocket_client.html<br/>Connected to WebSocket server.<br/>这个实例不如上面复杂，但服务端确实收到消息了：<br/>[2014-08-22 17:16:47]&nbsp;&nbsp; INFO&nbsp;&nbsp;&nbsp;&nbsp;new http request. fd=12<br/>[2014-08-22 17:16:47]&nbsp;&nbsp; INFO&nbsp;&nbsp;&nbsp;&nbsp;WebSocket connection #12 is connected<br/><br/><br/>（2）后端：websocket_client.php 这里可能要结合swoole_framework的类java包机制：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
define(&#039;DEBUG&#039;, &#039;on&#039;);
define(&quot;WEBPATH&quot;, str_replace(&quot;&#92;&#92;&quot;,&quot;/&quot;, __DIR__));
require __DIR__ . &#039;/../libs/lib_config.php&#039;;

$client = new Swoole&#92;Client&#92;WebSocket(&#039;127.0.0.1&#039;, 9503, &#039;/&#039;);
if(!$client-&gt;connect())
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;connect to server failed.&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;exit;
&#125;
while(true)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;$client-&gt;send(&quot;hello world&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;$message = $client-&gt;recv();
&nbsp;&nbsp;&nbsp;&nbsp;if($message === false) break;
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;Received from server: &#123;$message&#125;&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);
&#125;
echo &quot;Closed by server.&#92;n&quot;;
</textarea><br/>修改9503修改为9443，运行测试下：<br/>[root@jackxiang examples]# php&nbsp;&nbsp;websocket_client.php&nbsp;&nbsp;<br/>Received from server: Server: hello world<br/>Received from server: Server: hello world<br/>Received from server: Server: hello world<br/>Received from server: Server: hello world<br/>Received from server: Server: hello world<br/>Received from server: Server: hello world<br/>Received from server: Server: hello world<br/>^C<br/>得证。
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]WebSocket的JavaScript例子，结合swoole的PHP扩展及swoole_framework提供的example来编写并摸索备案。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>