Ping为您的Laravel框架提供了一个简单易用的消息或聊天或会话系统。

v0.1-p1 2017-07-31 07:59 UTC

This package is not auto-updated.

Last update: 2024-09-23 07:34:21 UTC


README

Ping Logo

简介

Ping为您的Laravel框架提供了一个简单易用的消息或聊天或会话系统。它易于集成和使用。

特性

  • 一对一聊天。
  • 发送消息无需检查会话。
  • 检查用户是否有权访问会话。
  • 会话未读消息计数(总数及个人)。
  • 自动标记为已读地检索消息。
  • 标记所有未读消息为已读(总数及个人)。
  • 从参与者端删除消息。
  • 永久删除消息和会话(硬删除)。
  • 消息加密(即将推出!)。
  • 群组聊天(即将推出!)。

安装

要开始使用Ping,请使用Composer将包添加到您的项目中

composer require harshaaliaschinna/ping

配置

安装Ping包后,在您的config/app.php配置文件中注册Harshaaliaschinna\Ping\PingServiceProvider

'providers' => [
    // Other service providers...

    Harshaaliaschinna\Ping\PingServiceProvider::class,
],

您不需要将Ping添加到别名数组中。我们已经为您完成了这项工作 💙。

迁移

在您的终端中运行以下命令,以发布此包所需的迁移文件。

php artisan vendor:publish --provider="Harshaaliaschinna\Ping\PingServiceProvider"

最后,运行以下命令执行迁移。

php artisan migrate

基本用法

示例1

// Ping::setId(from_id)->send(to_id, message);
Ping::setId(1)->send(2, "Hello, How are you?");

示例2

Ping::setId(1);
Ping::send(2, "Hello, How are you?");

示例3

namespace App\Controllers;

..
use Ping;

class Demo extends Controller {
    public function __construct() {
        // Ping once initialized it can be used anywhere without setting From Id.
        Ping::setId(Auth::Id());
    }
    
    public function sendMessage($toId, $message) {
        ..
        ..
        Ping::send($toId, $message);
        ..
    }
    
    public function retriveMessage($Id) {
        ..
        $markAsSeen = true; // Bool
        $message = Ping::recieve($Id, $markAsSeen);
        ..
    }
    ..
    ..
    ..
}

API参考

*注意:请注意,连接会话都是相同的。由于包名本身就指代了网络场景,因此使用了这些词仅为乐趣! 😛。

setId

此方法设置基本ID,称为base_user。请求可以从此ID进行。

object setId( int $id )

参数
Id:用于执行进一步请求的唯一ID。

返回值
返回Ping对象。错误时返回FALSE

send

向其他用户发送消息。如果用户之间没有连接,则此方法将自动创建新的连接。如果已经存在连接,它将使用该连接发送消息。

object send( int $to_id, string $message)

参数
to_id:要发送消息的唯一ID。
message:消息字段。

返回值
成功时返回对象。FALSE表示错误。
返回的对象包含新发送的消息ID ->id。它还包含连接ID ->connection_id以及其他一些内容。

new

在用户之间创建新的连接。

object new( int $user_one[, int $user_two = null])

参数
user_one:唯一ID。
user_two:如果此字段未设置,Ping将在通过setId()方法设置的base_user(用户)和user_one之间创建连接。否则,它将在user_one和user_two之间创建连接。

返回值
成功时返回对象。FALSE表示错误。
返回的对象包含连接ID ->id

exists

检查是否在base_user和提供的用户之间存在连接。

bool exists( int $user_two)

参数
user_two:唯一ID。此方法依赖于setId()

返回值
成功时返回对象。FALSE表示错误。

receive

使用消息ID检索消息。

object receive( int $message_id[, $seen = false])

参数
message_id:要检索消息的消息ID。
seen:将此消息标记为已读。

返回值
成功时返回对象。错误或未找到时返回FALSE

receiveAll

使用connection_id检索连接或对话中存在的所有消息。

object receiveAll( int $connection_id[[[[, bool $seen = false], string $order = 'ASC'], int $skip=null], int $take=null])

参数
connection_id: 连接ID。
seen: 通过base_user标记未读消息为已读。默认值为false

order: 使用消息时间戳按升序或降序排序。

skip: 跳过的消息数量。
take: 要检索的消息数量。

返回值
成功时返回对象。错误或未找到时返回FALSE

totalConnections

检索与base_user或提供的用户相关联的所有连接。

object totalConnections([int $user_id=null])

参数
user_id: 如果传递了user_id,Ping将基于提供的user_id检索所有连接。否则,它将使用base_user作为user_id并检索所有连接。

返回值
成功时返回对象。错误或未找到时返回FALSE

unreadCount

根据connection_id检索未读消息的数量。

int unreadCount( int $connection_id[, int $user_id=null])

参数
connection_id: 如果传递了user_id,Ping将根据提供的user_id检索所有未读消息的数量。否则,它将使用base_user作为user_id并检索数量。
user_id: 如果传递了user_id,Ping将其设置为base_user并检索未读消息数量。否则,它将使用base_user作为user_id并检索它。

返回值
成功时返回整数。错误或未找到时返回FALSE

markAsSeen

将消息标记为已读。

bool markAsSeen( int $message_id)

参数
message_id: 应传递消息ID。

返回值
成功时返回true。错误或未找到时返回FALSE

markUnreadAsSeen

使用connection_id将所有未读消息标记为已读。如果传递了user_id,它将标记该用户的特定收到的消息为已读。否则,它将标记该特定连接中的所有消息为已读。

bool markUnreadAsSeen( int $conection_id[, int $user_id = null])

参数
connection_id: 应传递特定的connection_id
user_id: 如果传递了user_id,它将标记该用户的收到的消息为已读。

返回值
成功时返回true。错误或未找到时返回FALSE

hasAccess

检查用户是否有权访问特定连接。

bool hasAccess( int $connection_id[, int $user_id = null])

参数
connection_id: 应传递特定的connection_id
user_id: 如果传递了user_id,Ping将检查给定的user_id是否有权访问连接。否则,它将使用base_user作为user_id并检查访问权限。

返回值
如果用户有权访问,则返回true。如果没有访问权限,则返回FALSE

delete

从一侧删除一条消息。

bool delete( int $message_id[, int $user_id=null])

参数
message_id: 应传递特定的message_id
user_id: 如果传递了user_id,Ping将从该用户一侧删除消息。否则,它将使用base_user作为user_id并从该用户一侧删除。

返回值
成功时返回true。错误或未找到时返回FALSE

hardDelete

消息将从数据库中永久删除。该连接中存在的任何用户都无法再次检索此消息。

bool hardDelete( int $message_id)

参数
message_id: 应传递特定的message_id

返回值
成功时返回true。错误或未找到时返回FALSE

hardDeleteAll

这将永久删除连接中的所有消息。换句话说,它将重置该连接。

bool hardDeleteAll( int $connection_id)

参数
connection_id: 应传递特定的connection_id

返回值
成功时返回true。错误或未找到时返回FALSE

hardDeleteConnection

这将永久删除连接及其中的消息。

bool hardDeleteConnection( int $connection_id)

参数
connection_id: 应传递特定的connection_id

返回值
成功时返回true。错误或未找到时返回FALSE

hardDeleteConnectionByUserId

这与hardDeleteConnection()相同,但此方法将接受user_id作为参数。将删除base_user和提供者user_id之间存在的关系和消息。

bool hardDeleteConnectionByUserId( int $user_id)

参数
user_id: 应传递特定的user_id

返回值
成功时返回true。错误或未找到时返回FALSE