参考链接
http://note.youdao.com/noteshare?id=867ae6fff10db8a457bcf428ce6b3003
Swoole初探.md
教程地址
安装
# 下载swoole git clone # 进入目录 cd ./swoole-src #切换版本 git checkout v4.2.2 # 编译安装 phpize clean #[php] Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environme #出现以上报错解决办法 apt-get install autoconf make clean phpize ./configure --enable-openssl --enable-sockets --enable-mysqlnd make make install # 找到php.ini路径 php -i |grep php.ini # 编辑php.ini vi /etc/php/7.0/cli/php.ini # 在文件中加入extension=swoole.so # 查看是否已经安装成功php -m# 如果找到 swoole 说明安装成功
Web 服务器
创建 swoole_http_server.php 写入:
$http = new swoole_http_server("0.0.0.0", 9501); $http->on('request', function ($request, $response) { var_dump($request->get, $request->post); $response->header("Content-Type", "text/html; charset=utf-8"); $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>"); }); $http->start();
启动
php swoole_http_server.php
开启进程守护(后台模式)
$this->server->set([ 'daemonize' => 1, ]);
关闭
先找到进程号
netstat -apn | grep 9503
杀死这个进程
kill -9 14323 # 14323 是进程号
************************************************************
http://note.youdao.com/noteshare?id=23fea7d594e1716ae3e9848ebbca366a
PHP Websocket 实现简易聊天系统.md
WebSocket是一个持久化的协议,举个简单的例子,http1.0的生命周期是以request作为界定的,也就是一个request,一个response,对于http来说,本次client与server的会话到此结束;而在http1.1中,稍微有所改进,即添加了keep-alive,也就是在一个http连接中可以进行多个request请求和多个response接受操作。然而在实时通信中,并没有多大的作用,http只能由client发起请求,server才能返回信息,即server不能主动向client推送信息,无法满足实时通信的要求。而WebSocket可以进行持久化连接,即client只需进行一次握手,成功后即可持续进行数据通信,值得关注的是WebSocket实现client与server之间全双工通信,即server端有数据更新时可以主动推送给client端。
演示地址 http://dev.topsts.cn/websocket.html
客户端实现(HTML5 JS)
<!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>WebSocket Test</title> <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> <script type="text/javascript" src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <script type="text/javascript"> $(function(){ if ("WebSocket" in window) { console.log("您的浏览器支持 WebSocket!"); // 打开一个 web socket var ws = new WebSocket("ws://dev.topsts.cn:9502"); console.log('连接状态:',ws); // 打开连接 ws.onopen = function() { $('#messages').append('WebSocket 连接成功 <br>'); }; // 接收到消息 ws.onmessage = function(evt) { var received_msg = evt.data; console.log('一个消息:',evt); $('#messages').append(evt.data+'<br>'); }; // 出现错误 ws.onerror = function(err) { console.warn('出现错误:',err); alert("出现错误"); } // 关闭 websocket ws.onclose = function() { console.log("连接已关闭..."); alert("连接已关闭..."); }; } else { // 浏览器不支持 WebSocket alert("您的浏览器不支持 WebSocket!"); } // 发送消息 $('#send').click(function() { var content = window.prompt('请输入内容', '默认内容'); ws.send(content); }); // 关闭连接 $('#close').click(function() { ws.close(); console.log("连接主动关闭"); }); }); </script></head><body> <div id="main"> <p>本页面用于测试WebSocket,使用ws://dev.topsts.cn:9502</p> <button id="send">发送数据</button> <button id="close">关闭连接</button> <p><br></p> <div id="messages">稍等,正在连接ws://dev.topsts.cn:9502<br></div> </div></body></html>
服务端实现 Swoole PHP
<?phpclass WebsocketTest{public $server;public function __construct(){$this->server = new swoole_websocket_server("0.0.0.0", 9502);$this->server->set([// 'daemonize' => 1, // 设置后台运行]);// 打开链接$this->server->on('open', function (swoole_websocket_server $server, $request) {echo "server: handshake success with fd{$request->fd}\n"; print_r($request->get); // get请求获取到的参数print_r($request->post); // post请求获取到的参数$this->server->push($request->fd, '系统:您的UID为' . $request->fd);foreach ($this->server->connections as $value) { $server->push($value, '广播:UID' . $request->fd . '用户已经上线'); } });// 接收数据$this->server->on('message', function (swoole_websocket_server $server, $frame) {echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";foreach ($this->server->connections as $value) {$this->server->push($value, '发送:(UID' . $frame->fd . ')' . $frame->data); } });// 关闭链接$this->server->on('close', function ($ser, $fd) {echo "client {$fd} closed\n";foreach ($this->server->connections as $value) {$this->server->push($value, '系统:UID' . $fd . '已经下线'); } });// 当做HTTP服务使用$this->server->on('request', function ($request, $response) {// 接收http请求从get获取message参数的值,给用户推送// $this->server->connections 遍历所有websocket连接用户的fd,给所有用户推送foreach ($this->server->connections as $fd) {$this->server->push($fd, $request->get['message']); } });$this->server->start(); } }new WebsocketTest();
启动
php index.php
即可
打开连接 $request swoole_http_request Object ( [fd] => 1 [header] => Array ( [host] => 192.168.99.100:9500 [connection] => Upgrade [pragma] => no-cache [cache-control] => no-cache [upgrade] => websocket [origin] => file:// [sec-websocket-version] => 13 [user-agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 [dnt] => 1 [accept-encoding] => gzip, deflate [accept-language] => zh-CN,zh;q=0.9 [sec-websocket-key] => q02QF0ZAGzm8cjLRuHsqlQ== [sec-websocket-extensions] => permessage-deflate; client_max_window_bits ) [server] => Array ( [query_string] => a=q&m=d [request_method] => GET [request_uri] => / [path_info] => / [request_time] => 1525428031 [request_time_float] => 1525428032.2676 [server_port] => 9500 [remote_port] => 63435 [remote_addr] => 192.168.99.1 [server_protocol] => HTTP/1.1 [server_software] => swoole-http-server ) [request] => [cookie] => [get] => Array ( [a] => q [m] => d ) [files] => [post] => [tmpfiles] => )
************************************************************
软件xshell
下载php-7.2.25.tar.bz2
链接: https://pan.baidu.com/s/17sgZ0aYIlVbdYHnH8LAeMg 提取码: qnir
使用 rz 命令上传到ubuntu服务器
进入php-7.2.25目录
执行以下三个命令
./configure
编译与编译安装
make && make install
设置配置文件
cp /usr/local/src/php-7.1.6/php.ini-production /usr/local/php7/etc/php.ini
接口文档编译器“