[实践OK]WebSocket的JavaScript例子,结合swoole的PHP扩展及swoole_framework提供的example来编写并摸索备案。
背景:swoole有一个Websocket示例,我作为swoole的顾问,群里反映cocos2dx下的websocket长连接,语言是不一样,但websocket是一样的,swoole是支持Websocket的,作下简单实践。
WebSocket的JavaScript例子,一个WebSocket的简单Echo例子:
例子代码来自:http://www.websocket.org/echo.html
上面这个链接可以当作一个websocket的工具调试并使用。
一、当然,得下载swoole服务扩展并安装:
git clone https://github.com/swoole/swoole-src.git
You should add "extension=swoole.so" to php.ini
二、动态服务端代码:
cd /data/codesdev/swoole/
git clone https://github.com/matyhtf/swoole_framework.git
/data/codesdev/swoole/swoole_framework/examples/websocket_server.php
上面是swoole_framework提供的代码,我们就直接运行这个服务端的server侦听9443端口:
php websocket_server.php
三、静态Js代码,通过firefox访问下,当然其他支持websocket的浏览器也可以,这块暂没从swoole_framework里摘,在网上摘的js,后面会补上从哪儿摘来的Url地址:
/data/htdocs/tools.jackxiang.com/websocket.html
vi websocket.html
四、在Firefox或chrome下访问这个支持websocket的Js代码:
WebSocket Test
CONNECTED
SENT: WebSocket rocks
RESPONSE: Server: WebSocket rocks
DISCONNECTED
说明这个php是真支持websocket的,否则不会输入什么就输出什么,得证。
参考:http://www.xyhtml5.com/websocket-javascript-example.html
——后来发现swoole_framework里的example里居然有对这个websocket的php配套的简单测试代码——
后记,其实swoole_framework下提供了和websocket相关的html测试及php客户端测试:
websocket_client.html websocket_client.php websocket_server.php
(1)前端:websocket_client.htm
把wss换成ws,wss好像是https的websocket:
//var wsServer = 'wss://127.0.0.1:9443'; //IP也换下
var wsServer = 'ws://119.10.6.23:9443';
访问下:http://tools.jackxiang.com/websocket_client.html
Connected to WebSocket server.
这个实例不如上面复杂,但服务端确实收到消息了:
[2014-08-22 17:16:47] INFO new http request. fd=12
[2014-08-22 17:16:47] INFO WebSocket connection #12 is connected
(2)后端:websocket_client.php 这里可能要结合swoole_framework的类java包机制:
修改9503修改为9443,运行测试下:
[root@jackxiang examples]# php websocket_client.php
Received from server: Server: hello world
Received from server: Server: hello world
Received from server: Server: hello world
Received from server: Server: hello world
Received from server: Server: hello world
Received from server: Server: hello world
Received from server: Server: hello world
^C
得证。
WebSocket的JavaScript例子,一个WebSocket的简单Echo例子:
例子代码来自:http://www.websocket.org/echo.html
上面这个链接可以当作一个websocket的工具调试并使用。
一、当然,得下载swoole服务扩展并安装:
git clone https://github.com/swoole/swoole-src.git
You should add "extension=swoole.so" to php.ini
二、动态服务端代码:
cd /data/codesdev/swoole/
git clone https://github.com/matyhtf/swoole_framework.git
/data/codesdev/swoole/swoole_framework/examples/websocket_server.php
上面是swoole_framework提供的代码,我们就直接运行这个服务端的server侦听9443端口:
php websocket_server.php
三、静态Js代码,通过firefox访问下,当然其他支持websocket的浏览器也可以,这块暂没从swoole_framework里摘,在网上摘的js,后面会补上从哪儿摘来的Url地址:
/data/htdocs/tools.jackxiang.com/websocket.html
vi websocket.html
四、在Firefox或chrome下访问这个支持websocket的Js代码:
WebSocket Test
CONNECTED
SENT: WebSocket rocks
RESPONSE: Server: WebSocket rocks
DISCONNECTED
说明这个php是真支持websocket的,否则不会输入什么就输出什么,得证。
参考:http://www.xyhtml5.com/websocket-javascript-example.html
——后来发现swoole_framework里的example里居然有对这个websocket的php配套的简单测试代码——
后记,其实swoole_framework下提供了和websocket相关的html测试及php客户端测试:
websocket_client.html websocket_client.php websocket_server.php
(1)前端:websocket_client.htm
把wss换成ws,wss好像是https的websocket:
//var wsServer = 'wss://127.0.0.1:9443'; //IP也换下
var wsServer = 'ws://119.10.6.23:9443';
访问下:http://tools.jackxiang.com/websocket_client.html
Connected to WebSocket server.
这个实例不如上面复杂,但服务端确实收到消息了:
[2014-08-22 17:16:47] INFO new http request. fd=12
[2014-08-22 17:16:47] INFO WebSocket connection #12 is connected
(2)后端:websocket_client.php 这里可能要结合swoole_framework的类java包机制:
修改9503修改为9443,运行测试下:
[root@jackxiang examples]# php websocket_client.php
Received from server: Server: hello world
Received from server: Server: hello world
Received from server: Server: hello world
Received from server: Server: hello world
Received from server: Server: hello world
Received from server: Server: hello world
Received from server: Server: hello world
^C
得证。
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/7418/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2014-8-22 22:22
评论列表