服务端安装
composer require workerman/workerman
客户端安装
composer require textalk/websocket
<?php
namespace App\Controllers;
use Workerman\Worker;
use Workerman\Lib\Timer;
use WebSocket\Client;
class Workerman extends BaseController
{
protected $uidConnections = [];
protected $HEARTBEAT_TIME = '60';
public $code_arr = [
'100' => ['code'=>100, 'msg'=>'连接成功'],
'101' => ['code'=>101, 'msg'=>'登录成功'],
'102' => ['code'=>102, 'msg'=>'发送数据'],
'103' => ['code'=>103, 'msg'=>'接收成功'],
'131' => ['code'=>131, 'msg'=>'发送失败'],
];
protected $worker = null;
public $count = 0;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'workerman:websocket';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
*
* @var string
*/
protected $redis = '';
/**
* Create a new command instance.
*
* @return void
*/
// public function __construct()
// {
// parent::__construct();
// }
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->start();
}
public function start()
{
$this->worker = new Worker('websocket://0.0.0.0:1818');
$this->onConnect();
$this->onMessage();
$this->onClose();
$this->redis = $this->redisdb();
Worker::runAll();
}
public function onConnect() {
$this->worker->onConnect = function($connection) {
$connection->send(json_encode($this->code_arr['100']));
$this->count++;
$msg = "新的连接 目前连接数 {$this->count}";
var_dump($msg);
log_message('error',$msg);
};
}
public function onMessage() {
$this->worker->onMessage = function ($connection, $data){
$connection->lastMessageTime = time(); //更新上一次会话时间
//客户端心跳 直接返回
if ($data == "") {
return;
}
$data = json_decode($data, true);
if (empty($data['uid'])) {
$connection->close();
return;
}
$uid = $data['uid'];//这里的uid根据自己的情况去验证
switch ($data['type']) {
case 'login':
// 保存该用户的输送数据
$connection->uid = $uid;
$this->uidConnections[$uid] = $connection;
$msg = "uid:{$uid} 登录成功 目前登录数".count($this->uidConnections);
$r = $this->redis->get('ddd:'.$uid);
if($r){
$this->redis->delete('ddd:'.$uid);
}
log_message('error',$r.'---');
// var_dump($msg);
log_message('error',$msg);
$connection->send(json_encode(['sss'=>$r]));
break;
case 'send':
// 发送消息
$res = $this->sendMessageByUid($uid, $data['info']);
$msg = "发送成功";
if (!$res) {
$msg = "发送失败";
}
// var_dump($msg);
log_message('error',$msg);
break;
case 'message':
break;
}
};
}
// 针对uid推送数据
public function sendMessageByUid($uid, $message)
{
if(isset($this->uidConnections[$uid]))
{
$send_data = $this->code_arr['102'];
$send_data['info'] = $message;
$this->uidConnections[$uid]->send(json_encode($send_data));
return true;
}
return false;
}
public function onClose() {
$this->worker->onClose = function ($connection){
$this->count--;
if(isset($connection->uid)) {
$msg = "uid:{$connection->uid} 已断开";
var_dump($msg);
log_message('error',$msg);
// Log::write("$msg", 'WorkerManServer');
// 连接断开时删除映射 和 二维码图片
unset($this->uidConnections[$connection->uid]);
(new OtherService())->delLoginQrCode($connection->uid);
}
$msg = "连接已断开 目前连接数 {$this->count}";
var_dump($msg);
log_message('error',$msg);
};
}
public function redisdb($db=0)
{
$redis=new \redis();
$redis->connect("127.0.0.1","6379");
$redis->select($db);
return $redis;
}
public function test($value='')
{
$data = array(
'uid' => 1,
'type' => 'send',
'info' => ['sss'=>11]
);
$json = json_encode($data);
$client = new Client("ws://127.0.0.1:1818");
$client->send($json);
$message = $client->receive();
log_message('error',$message);
$client->close();
}
}
?>
服务端执行
Workerman/handle;方法
客户端请求链接
Workerman/test
前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="margin: 0 auto;width: 800px">
<h2>聊天室</h2>
<textarea type="text" style="border: 1px solid black;width: 800px;height: 200px;" id="his"></textarea>
<input type="text" style="width:740px" id="sendMsg"> <button id="send">发送</button>
</div>
</body>
</html>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function () {
var ws = new WebSocket("ws://127.0.0.1:1818");
$('#send').click(function () {
msg = $('#sendMsg').val();
$('#sendMsg').val('');
ws.send(msg);
});
ws.onopen = function() {
$('#his').append("服务器连接成功.....");
$('#his').append("\n");
};
ws.onmessage = function(e) {
$('#his').append(e.data);
$('#his').append("\n");
};
});
</script>
https的看这个: