原标题:还在用Ajax轮询完成即时通讯?看看Swoole吧
关键词:swoole
swoole是什么?
1.异步、并行、高性能
2.纯C编写
3.php扩展
swoole能做什么?
1.异步多线程服务器及客户端,
2.异步MySQL、Redis、数据库连接池、任务队列
3.http/websocket服务器/客户端
4.异步文件读写
5.Swoole2.0支持协程
swoole应用到那里?
互联网、移动通信、企业软件、云计算、网络游戏、物联网、车联网
swoole应用案例
IM聊天:http://im.classba.com.cn/main.php
战旗TV:http://www.zhanqi.tv
虎牙直播:http://www.huya.com
YY语音:http://www.yy.com/
01
swoole安装需求
1.服务器版本:centos7/ubuntu16
2.php版本:php5.4
phpize
./configuremake
sudo make install
系统就会自动安装,如果提示phpize不存在的话,就需要安装一下phpize.
另一种方式直接执行
pecl install swoole
安装完成,需要更改php.ini的配置,将extension=swoole.so
php -m 查看是否有swoole扩展
Swoole创建TCP服务器
new swoole_server(string $host, int $port, int $mode = SWOOLE_PROCESS,int $sock_type = SWOOLE_SOCK_TCP);
bool swoole_server->on(string $event, mixed $callback);
bool swoole_server->start() 开启TCP服务器
bool swoole_server->send(int $fd, string $data, int $reactorThreadId = 0);
function swoole_server->set(array $setting);
02
Swoole创建WEB服务器
swoole_http_response->header(string $key, string $value);
swoole_http_response->cookie(string $key, string $value = '', int $expire = 0 , string $path = '/', string $domain = '', bool $secure = false , bool $httponly = false);
swoole_http_response->status(int $http_status_code);
swoole_http_response->gzip(int $level = 1);
bool swoole_http_response->write(string $data);
function swoole_http_response->sendfile(string $filename, int $offset = 0, int $length = 0);
swoole_http_response->end(string $html);
swoole_http_request
http请求对象,保存了Http客户端请求的相关信息,包括GET、POST、COOKIE、Header等。
03
Swoole搭建聊天室
server.php服务器文件
//创建websocket服务器对象,监听0.0.0.0:9502端口
$ws = new swoole_websocket_server("0.0.0.0", 9502);
//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {
$GLOBALS['fd'][$request->fd]['id'] = $request->fd;// 设置用户ID 安顺序递增
$GLOBALS['fd'][$request->fd]['name'] = '匿名用户';// 设置用户名
});
//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
$msg = $GLOBALS['fd'][$frame->fd]['name'].":{$frame->data}n";
if(strstr($frame->data,'#name#')){// 用户设置昵称
$GLOBALS['fd'][$frame->fd]['name'] = str_replace('#name#','',$frame->data);
}else{// 普通发送用户信息
foreach($GLOBALS['fd'] as $i){// 发送数据到客户端
$ws->push($i['id'],$msg);
}
}
});
//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
echo "客户端-{$fd} 断开连接n";
unset($GLOBALS['fd'][$fd]);// 清除 已经关闭的客户端
});
$ws->start();
前台页面
IM< type="text/java"src="static/js/jquery-3.2.0.min.js">>
< type="text/java" src="static/js/webSocket.js">>
< type="text/java" src="static/js/index.js">>
webSocket.js文件
var msg = document.getElementById("msg");
var wsServer = 'ws://192.168.50.151:9502';//调用websocket对象建立连接://参数:ws/wss(加密)://ip:port (字符串)
var websocket = new WebSocket(wsServer);
websocket.onopen = function (evt) {//onopen监听连接打开
// 应该显示远程服务器连接成功
//msg.innerHTML = websocket.readyState;
//websocket.readyState 属性:
/*
CONNECTING 0 The connection is not yet open.
OPEN 1 The connection is open and ready to communicate.
CLOSING 2 The connection is in the process of closing.
CLOSED 3 The connection is closed or couldn't be opened.
*/
};
//onmessage 监听服务器数据推送
websocket.onmessage = function (evt) {
msg.innerHTML += evt.data +'
';//不断递增的数据
console.log('从服务器获取到的数据: ' + evt.data);
};
//监听连接关闭
websocket.onclose = function (evt) {
console.log("服务器拒绝");
};
//监听连接错误信息
websocket. = function (evt, e) {
console.log('错误: ' + evt.data);
};
//发送信息
function send_msg(){
var text = document.getElementById('text').value;// 获取数据
document.getElementById('text').value = '';// 清空数据
websocket.send(text);//向服务器发送数据
}
//发送昵称
function send_name(){
var text = document.getElementById('myname').value;// 获取数据
websocket.send("#name#"+text);//向服务器发送数据
var myTitle = document.getElementById("myTitle");
myTitle.innerHTML = "IM "+text;
alert("设置成功");
var setName = document.getElementById("setName");
setName.style.display = "none";
var send_msg = document.getElementById("send_msg");
send_msg.style.display = "block";
}
04
大致效果呈现
05
福利时间到
学无止境,给各位热爱编程的小伙伴带来一个福利:
免费PHP技术交流群
进群即可领取源代码
免费PHP技术交流群,不定期会有初始redis、大型社交网站加密算法、自定义MVC框架、百万数据索引优化等免费知识分享课。
如果你对程序员这份工作满怀热爱,如果你乐意走在技术的最前沿,不妨加入我们,将所有的迷惘一扫而光吧。返回搜狐,查看更多
责任编辑: