zaichaopan/online-status 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zaichaopan/online-status

Composer 安装命令:

composer require zaichaopan/online-status

包简介

A package to show user online status in your laravel app

README 文档

README

This packages adds functionality to show user online status, online users and online user number in your laravel app. This package can be used in laravel 5.5 or higher.

Installation

This packages uses redis behind the screen. So please install redis in your machine in order to use package.

composer require zaichaopan/online-status

Usage

  • Add HasOnlineStatus trait to your user model
//...
use Zaichaopan\OnlineStatus\HasOnlineStatus;

class User extends Model
{
    use HasOnlineStatus;

}
  • Set online expiration time.

This package uses lifetime in config/session.php as the default user online expiration time. If user remains inactive during this time period, he or she will be considered offline. So if you want to customize. you can override it in config/session.php or you can override it in User model as follow

// ...
class User extends Model
{
    use HasOnlineStatus;

    public static function getOnlineExpirationInMinutes(): int
    {
        return 10;
    }
}
  • Apply UserOnline Middleware.php
// App\Http\Kernel.php
class Kernel extends HttpKernel
{
    // ...
    protected $middlewareGroups = [
        'web' => [
           \\...
           \Zaichaopan\OnlineStatus\Middleware\UserOnline::class
        ],
        //...
    ];
}

Now when authenticated user makes a web request, his or her online status will be automatically be set.

//
class UserOnline
{
   // ...
    public function handle(Request $request, Closure $next)
    {
        optional($request->user())->online();

        return $next($request);
    }
}

Available Apis

  • Get whether user is online:
$status = $user->isOnline;

// or
$status = $user->isOnline();
  • Get total online user number
$onlineUserCount = User::onlineCount();
  • Get online users
$onlineUsers = User::ofOnline()->get();

// or
$onlineUsers = User::ofOnline()->paginate();
  • Set user online

As long as you apply the UserOnline middleware properly, it will automatically set and update authenticated user online status. In case you want to set it manually, you can use online method provided by the trait.

$user->online();
  • Set user offline

This package listens for Logout event. When user logs out, it will set user online status as offline. In case you may want to set user offline manually, you can use offline method provided by the trait.

$use->offline();

Events

This packages raises two events during the updating user online status process. You may attach listeners to these two events in your EventServiceProvider:

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'Zaichaopan\OnlineStatus\Events\Online' => [
        'App\Listeners\Online',
    ],

    'Zaichaopan\OnlineStatus\Events\Offline' => [
        'App\Listeners\Offline',
    ]
];

The Online event will only fire once. It won't fire again if the user is already online

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-06-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固