websocket账号密码登录php,PHP实现微信扫码登陆(四)--使用workerman搭建websocket服务器...

本文介绍了如何在 Laravel 项目中利用 Artisan 命令和 Workerman 框架创建一个 WebSocket 服务器,以实现后端消息推送。通过监听端口并处理客户端连接,当微信登录完成后,服务器能够接收到消息并将其推送给浏览器。此外,还展示了如何处理内部连接和浏览器连接的关闭。通过这种方式,可以实现微信登录状态的实时更新。
摘要由CSDN通过智能技术生成

因为使用laravel, 所以使用artisan编写服务,

也可以使用其他文件编写, 直接用php 命令执行, 参照博客

PHP使用workerman实现后端消息推送前端, 完成微信登录

建议先读一下上边这边博客, 有助于读者理解整个推送过程

代码如下

app/Console/Commands/WebsocketServer

namespace App\Console\Commands;

use Illuminate\Console\Command;

use Workerman\Worker;

use Log;

class WebsocketServer extends Command

{

private $worker = null;

/**

* The name and signature of the console command.

*

* @var string

*/

protected $signature = 'websocket {action}';

/**

* The console command description.

*

* @var string

*/

protected $description = 'start websocket server for accept client connect';

/**

* Create a new command instance.

*

* @return void

*/

public function __construct()

{

parent::__construct();

}

/**

* Execute the console command.

*

* @return mixed

*/

public function handle()

{

global $argv;

$arg = $this->argument('action');

$argv[1] = $argv[2];

$argv[2] = isset($argv[3]) ? "-{$argv[3]}" : '';

switch ($arg) {

case 'start':

$this->start();

break;

case 'stop':

break;

case 'restart':

break;

case 'reload':

break;

case 'status':

break;

case 'connections':

break;

}

}

private function start(){

// 使用websocket协议, 监听端口, 等待客户端发起websocket连接

$this->worker = new Worker('websocket://0.0.0.0:18888');

// 保存客户端连接

$this->worker->uidConnections = [];

$this->worker->count = 1;

$this->worker->onWorkerStart = function ($worker) {

// 创建内部连接, 用于接收扫码后, php服务器推送的消息

$inner_text_worker = new Worker('tcp://0.0.0.0:18889');

$inner_text_worker->onMessage = function ($connection, $buffer) {

Log::alert('workerman inner accept message : ' . $buffer);

$data = json_decode($buffer, true);

$uid = $data['scene'];

$token = $data['token'];

// 向浏览器推送用户标识

$res = $this->sendMessageByUid($uid, $token);

Log::alert('workerman reply message to uid : '. $uid .'..$openid..'. $token);

$mess = $res ? 'ok' : 'fail';

Log::alert('workerman reply message : ' . $mess);

// 通知php客户端推送结果

$connection->send($mess);

};

$inner_text_worker->listen();

};

$this->worker->onMessage = function ($connection, $data) {

Log::alert('workerman accept browser data : ' . $data);

if (!isset($connection->uid)) {

$connection->uid = $data;

$this->worker->uidConnections[$connection->uid] = $connection;

return;

}

};

$this->worker->onClose = function ($connection) {

if (isset($connection->uid)) {

Log::alert('workerman close browser : ' . $connection->uid);

unset($this->worker->uidConnections[$connection->uid]);

}

};

Worker::runAll();

}

/**

* 消息广播

* @param $message

*/

private function broadCast($message)

{

global $worker;

foreach ($worker->uidConnections as $connection) {

$connection->send($message);

}

}

private function sendMessageByUid($uid, $message)

{

Log::alert(json_encode($this->worker->uidConnections));

if (isset($this->worker->uidConnections[$uid])) {

$connection = $this->worker->uidConnections[$uid];

$connection->send($message);

return true;

}

return false;

}

}

在app/Console/Kernel.php中注册

protected $commands = [

//

WebsocketServer::class,

];

进入到项目根目录, 启动, 等待连接

php artisan websocket start

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值