目的:学习swoole,众所周知php是单线程语言,所以多进程是瓶颈,但是swoole的出现解决了这个问题,为了以后解决并发和大数据问题,特学习。workman也同swoole一样可以解决这个问题,workman是基于swoole开发的。
坏境:自行配置PHP环境和swoole扩展(swoole不支持Windows,可以用Linux的模拟工具,swoole官网有详细介绍 进入swoole官网)
聊天室效果:
1.新用户进入显示欢迎语,默认为用户设置默认昵称
2.可以自行设置昵称
3.发送消息,在聊天室里进行对话
4.用户离开聊天室时显示用户离开信息
代码部分:
swoole server端代码(server端需要在cli模型下运行)
/**
* Created by PhpStorm.
* User: zhengjiayu
* Date: 2019/3/5
* Time: 下午1:22
*/
error_reporting(E_ALL); ini_set("display_errors", 1);
class Swooleserver
{
private static $instance; //单例对象
private static $server; //服务器对象
public function __construct()
{
self::$server = new swoole_websocket_server("127.0.0.1", 9502);
//绑定事件
self::$server->on('open',[$this,'onOpen']);
self::$server->on('message',[$this,'onMessage']);
self::$server->on('close',[$this,'onClose']);
}
/**
* 创建单例对象
* @return Swoole_server
*/
public static function getInstance()
{
if(!self::$instance instanceof self){
self::$instance = new self();
}
return self::$instance;
}
/**
* 开启服务
*/
public function start(){
self::$server->start();
}
/**
*当客户端连接服务器时执行
* @param $server 服务器
* @param $req 客户端
*/
public function onOpen($server, $req)
{
$name = '用户'.rand(1,99999);
$_SESSION['user'] = $name;
$data = [
'action' => 'open',
'data' => $name."欢迎进入聊天室",
'user' => $name
];
foreach($server->connections as $fd)
{
self::$server->push($fd, json_encode($data));
}
}
/**
* 当客户端给服务器发送消息时执行
* @param $server 服务器
* @param $frame 接受的数据
*/
public function onMessage($server, $frame)
{
$data = json_decode($frame->data,true);
switch ($data['action']){
case 'setname':
$_SESSION['user'] = $data['data'];
break;
case 'send':
$data['data'] = $_SESSION['user'].":".$data['data'];
break;
}
$data['user'] = $_SESSION['user'];
foreach($server->connections as $fd)
{
self::$server->push($fd, json_encode($data));
}
}
/**
* 当客户端与服务器断开连接时执行
* @param $server 服务器
* @param $fd 客户端ID
*/
public function onClose($server, $fd)
{
$data['action'] = 'close';
$data['data'] = $_SESSION['user'].'离开聊天室';
foreach($server->connections as $fd)
{
self::$server->push($fd, json_encode($data));
}
}
}
Swooleserver::getInstance()->start();
swoole 客户端代码
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>swoole 聊天室测试
<link href="./tool/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
#container
{
text-align:center;
margin-top:68px;
}
.content
{
width:600px;
height:300px;
border:1px solid #b3b3b3;
margin-left:144px;
border-radius:2%;
}
<div id="container">
<h1>聊天室
<div class="content">
<div class="input-append">
<input class="span2" id="setname" type="text">
<button class="btn" type="button" id="setnickname">设置昵称
<div class="input-append">
<input class="span2" id="send" type="text">
<button class="btn" type="button" id="sendmsg">发送
<script src="https://code.jquery.com/jquery-3.1.1.min.js">
<script src="./tool/bootstrap/js/bootstrap.min.js">
// 初始化一个 WebSocket 对象
var ws =new WebSocket("ws://localhost:9502/");
// 建立 web socket 连接成功触发事件
ws.onopen =function () {
// 使用 send() 方法发送数据
// ws.send("发送数据");
// alert("连接成功...");
};
// 接收服务端数据时触发事件
ws.onmessage =function (evt) {
var received_msg = JSON.parse(evt.data);
if(received_msg.action =='open'){//展示用户欢迎语,给用户随机昵称
$('.content').append('<p><small>'+received_msg.data+'</small></p>');
}else if(received_msg.action =='setname'){
// $('#setname').val(received_msg.data);
}else if(received_msg.action =='send'){
$('.content').append('<ul>'+received_msg.data+'</ul>');
}else if(received_msg.action =='close'){
$('.content').append('<p><small>'+received_msg.data+'</small></p>');
}
};
// 断开 web socket 连接成功触发事件
ws.onclose =function (evt) {
};
//设置昵称
$("#setnickname").click(function(){
ws.send('{"action":"setname","data":"'+$('#setname').val()+'"}');
});
//发送消息
$('#sendmsg').click(function () {
ws.send('{"action":"send","data":"'+$('#send').val()+'"}');
});
</html>
优化:界面显示+踢人