nativescript-laravel-echo
为 Laravel-Echo 开发的 NativeScript 插件。
npm i --save nativescript-laravel-echo

NativeScript Laravel-Echo

npm npm

NPM

这是为使用 NativeScript 开发的原生应用程序制作的 laravel-echo 插件。

有关更多信息,请阅读 Laravel 广播

先决条件 / 要求

在您的 Laravel 后端应用程序中需要必要的 API 认证。

修改包含以下行的文件 Broadcast::routes()

示例在 app/Providers/BroadcastServiceProvider.php

class BroadcastServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Broadcast::routes(['middleware' => 'auth:api']);
require base_path('routes/channels.php');
}
}

安装

描述您的插件安装步骤。理想情况下,它可能类似于

$ tns plugin add nativescript-laravel-echo

使用方法

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class Event implements ShouldBroadcast
{
/**
* Information about the shipping status update.
*
* @var string
*/
public $data; //data show in console.dir as data
}

JavaScript

const TnsEcho = require('nativescript-laravel-echo').TnsEcho;

const token = 'Asdsd3dsdsTytf';//Your token

const options = {
//....
broadcaster: 'socket.io',// pusher,
headers: {
auth: {
bearer: `Bearer ${token}`
}
}
}

const Echo = new TnsEcho(options)

Echo.channel('YourChannel').listen('Event', e => {
console.dir(e)
})

//presence channel for others
const http = require('tns-core-modules/http');

http.request({
//....
headers: {
//...
'X-Socket-Id': Echo.socketId() // get socket id
}
});

TypeScript

import { TnsEcho } from 'nativescript-laravel-echo';

const token = 'Asdsd3dsdsTytf';//Your token

const options = {
//....
broadcaster: 'socket.io',// pusher,
headers: {
auth: {
bearer: `Bearer ${token}`
}
}
}

this.Echo = new TnsEcho(options)

this.Echo.channel('YourChannel').listen('Event', e => {
console.dir(e)
})

//presence channel for others
import * as http from 'tns-core-modules/http';

http.request({
//....
headers: {
//...
'X-Socket-Id': this.Echo.socketId() // get socket id
}
})

选项

这些是对象选项中可以包含的每个参数。

属性 默认值 描述
broadcaster pusher 广播推送器或 socket.io 的驱动程序
host null 仅适用于 socket.io 的您的宿主
authEndpoint /broadcasting/auth 仅适用于 Pusher 的您的认证端点
key null 您的 Pusher 应用程序的 API 密钥
cluster null 您的 Pusher 应用程序的集群
auth auth:{headers:{}} 对于需要带有 Authorization 标头的私有和存在通道,这是必需的
namespace App.Events 后端事件的空间
debug false 仅启用 socket.io 广播推送器的调试
forceWebsockets false 强制使用 WebSockets

许可证

Apache 许可证版本 2.0,2004 年 1 月