laravel-websockets使用教程

后端

1、安装扩展

composer require beyondcode/laravel-websockets

发布迁移

php artisan vendor:publish --provider="BeyondCode\LaravelWebSockets\WebSocketsServiceProvider" --tag="migrations"
php artisan migrate

发布配置

php artisan vendor:publish --provider="BeyondCode\LaravelWebSockets\WebSocketsServiceProvider" --tag="config"

配置 .env

PUSHER_APP_ID=
PUSHER_APP_KEY=your-pusher-key
PUSHER_APP_SECRET=
PUSHER_HOST=127.0.0.1
PUSHER_PORT=6001
PUSHER_SCHEME=http
PUSHER_APP_CLUSTER=mt1

2、确保 config/app.php 文件,providers 配置中没有注释依赖项。如果有不能解释的报错,可以确认一下。

3、创建事件

php artisan make:event DemoEvent

修改类 App\Events\DemoEvent ,注意添加实现 ShouldBroadcast

<?php

namespace App\Events;

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

class DemoEvent implements ShouldBroadcast # 添加实现类
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public string $message;

    // 使用成员变量,前端 listen 事件名为 `DemoEvent`
    // public $broadcastAs = 'DemoEvent';

    public function __construct()
    {
        $this->message = 'Hello friend';
    }

    // 使用成员方法,前端 listen 事件名为 `.DemoEvent`
    public function broadcastAs() {
        return 'DemoEvent';
    }

    public function broadcastOn()
    {
        // PrivateChannel 需要登录,改为 Channel
        // channel 名称本文改为 public
        return new Channel('public');
    }
}

如果不指定 broadcastAs,默认事件名为类名,即: App\Events\DemoEvent

4、运行服务

php81 artisan websockets:serve

5、调用/下发事件

# 以下都行
DemoEvent::dispatch();
broadcast(new DemoEvent())->toOthers();
broadcast(new DemoEvent());
event(new DemoEvent());

6、DASHBOARD

访问 /laravel-websockets 可以打开 dashboard 和调试,地址可以在 config/websockets.php 中配置

前端

1、以 vue 为例,安装扩展

npm i -D laravel-echo pusher-js

2、添加 laravel-echo.js 文件

import Echo from "laravel-echo";
import Pusher from "pusher-js";
window.Pusher = Pusher;

window.Echo = new Echo({
  broadcaster: "pusher",
  key: "your-pusher-key",
  wsHost: window.location.hostname,
  wsPort: 6001,
  forceTLS: false,
  disableStats: true,
  cluster: "mt1",
});

main.js 中引入

import "./laravel-echo";

3、在合适的位置订阅消息

window.Echo.channel("public").listen(".DemoEvent", (e) => {
  console.log(e.message);
});

此处 DemoEvent 为事件名,在后端可以通过 broadcastAs 来指定:

  1. 如果没有指定,默认为类名: App\\Events\\DemoEvent
  2. 如果使用成员变量:DemoEvent
  3. 如果使用成员方法:.DemoEvent
继续阅读

删除git中的大文件历史

列出所有仓库中的对象(包括SHA值、大小、路径等),并按照大小降序排列,列出TOP 10:

git rev-list --all | xargs -rL1 git ls-tree -r --long | sort -uk3 | sort -rnk4 | head -10

根据最大文件的路径,修改此文件的commit历史:

git filter-branch -f --tree-filter "rm -f <filepath>" -- --all

强制提交到远程分支:

git push -f

sublime-merge 中文化的方法

菜单中选择 Preferences > Browse Packages… ,在弹出的文件夹中,克隆 git 仓库:

git clone https://github.com/sublime-china/sublime-merge-chinese.git Default

重启软件即可。

ubuntu 20.04+使用netplan设置静态IP

编辑文件 /etc/netplan/00-installer-config.yaml ,修改为以下格式:

network:
  ethernets:
    ens33:   # 配置的网卡的名称
      optional: true # 可选接口,找不到时不会报错
      addresses: [192.168.253.133/24]
      dhcp4: false   # 关闭dhcp4
      gateway4: 192.168.253.2
      nameservers:
        addresses: [192.168.253.2,114.114.114.114]
  version: 2

PVE系统升级的方法

默认情况下,PVE只升级debian的系统,不会升级PVE管理,要同一起升级,需要添加 No-Subscription 库,具体步骤如下。

  1. 添加 No-Subscription 库

在左侧菜单中,选择一个节点,然后在管理菜单中单击“更新”下的“软件源”(Repositories),点击“添加”按钮,选择“No-Subscription”。

  1. 升级

在管理菜单中点击“更新”,点击“刷新”,完成后点击“升级”即可。

  1. 完成升级

升级完成后,可能需要重启服务器。

docker 无法启动 mysql 容器可能的解决办法

启动 mysql:8.* 效果很好,但 mysql:5.7.* 会导致立即 100% 内存消耗,尝试通过以下方法解决。

方法1

  1. 编辑 /lib/systemd/system/containerd.service ,修改 LimitNOFILE=infinityLimitNOFILE=1048576
  2. sudo systemctl daemon-reload
  3. sudo systemctl restart containerd.service

再尝试启动 mysql 容器。

方法2

  1. 编辑 /etc/docker/daemon.json
{
    // 原有配置...
    "default-ulimits": {
        "nofile": {
            "Name": "nofile",
            "Hard": 64000,
            "Soft": 64000
        }
    }
}

2. 重启服务 systemctl restart docker

参考:https://sukbeta.github.io/docker-ulimit-configure/

打印 apk 中的信息

前提是安装了 android studio,在 linux 中,使用以下命令:

~/Android/Sdk/build-tools/33.0.2/aapt dump <app.apk>

其中,33.0.2 要改成自己实际的版本。

在manjaro/archlinux中设置dbeaver指定的java版本

找到 dbeaver 的执行位置,查看它的安装位置:

$ which dbeaver
/usr/bin/dbeaver
$ cat /usr/bin/dbeaver
export GTK_OVERLAY_SCROLLING=0
/usr/lib/dbeaver/dbeaver $@

进入 /usr/lib/dbeaver/ 目录,编辑 dbeaver.ini ,在 -vmargs 上面,添加以下内容:

-vm
/usr/lib/jvm/java-17-openjdk/bin

注意:确保已经安装了dbeaver指定的 jre/jdk 版本,并获取其目录。

docker容器的重启策略

Use a restart policy

To configure the restart policy for a container, use the --restart flag when using the docker run command. The value of the --restart flag can be any of the following:

FlagDescription
noDo not automatically restart the container. (the default)
on-failure[:max-retries]Restart the container if it exits due to an error, which manifests as a non-zero exit code. Optionally, limit the number of times the Docker daemon attempts to restart the container using the :max-retries option.
alwaysAlways restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted. (See the second bullet listed in restart policy details)
unless-stoppedSimilar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.

参考:https://docs.docker.com/config/containers/start-containers-automatically/