php使用elephant.io和nodejs的socket.io通信

8 篇文章 1 订阅
7 篇文章 0 订阅

elephant.io 里面已经封装好啦socket0.x 1.x 2.x对应的类安装最新版本就可以 github地址 https://github.com/wisembly/elephant.io

使用composer安装,打开composer.json加入下面包进行安装

"require": {
	 "wisembly/elephant.io": "~3.0"
},

使用方法

use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version2X;

	public function test(){
        $client = new Client(new Version2X('https://****:19967', [
            'headers' => [
                'X-My-Header: websocket rocks',
                'Authorization: Bearer 12b3c4d5e6f7g8h9i',
            ],
            //连接超时时间
            'timeout' => 5000,
        ]));
        
         //初始化并建立连接
        $client->initialize();

		//发送一个conn请求
        $client->emit('conn', ['uid' => 81252826, 'token' => '9917d7eb298ca324ed633325cb040c18', 'roomnum' => 6, 'stream' => '6_1603425280']);
        
        $msg = [
            'msg' => [
                [
                    '_method_' => 'SendMsg',
                    'action' => 0,
                    'ct' => '聊天消息内容',
                    'msgtype' => '2',
                    'tougood' => '',
                    'touid' => '',
                    'touname' => '',
                    'ugood' => '这里填用户id',
                    'uid' => '这里填用户id',
                    'uname' => '小美',
                    'avatar_thumb' => 'http://*******/default_thumb.jpg',
                    'level' => '1',
                    'vip_type' => '0',
                    'liangname' => '0',
                    'usertype' => '30',
                    'guard_type' => '0',
                ],
            ],
            'retcode' => '000000',
            'retmsg' => 'OK',
        ];
        
        //发送一个broadcast请求
        $client->emit('broadcast',$msg);

        //断开连接
        $client->close();
    }

经过测试可用,但是连接效率不是很高,小场景可以考虑使用,否则还是比较建议使用nodejs做客户端进行相关业务操作…

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Laravel 中整合 PHPSocket.io 实现 web 消息推送,你需要遵循以下步骤: 1. 安装 PHPSocket.io 你可以使用 Composer 包管理器安装 PHPSocket.io。在终端中,导航到 Laravel 项目目录并运行以下命令: ``` composer require elephant-io/elephant.io ``` 2. 配置 PHPSocket.io 在 Laravel 项目中,你需要在 `config/app.php` 文件中添加以下行: ```php 'aliases' => [ // ... 'ElephantIO' => 'ElephantIO\Engine\SocketIO\Version1X', ], ``` 3. 创建 PHPSocket.io 服务提供者 在 Laravel 项目中,你需要创建 PHPSocket.io 服务提供者。运行以下命令来创建: ``` php artisan make:provider SocketIoServiceProvider ``` 在 `SocketIoServiceProvider` 类中,你需要添加以下内容: ```php use ElephantIO\Client; use ElephantIO\Engine\SocketIO\Version1X; public function register() { $this->app->singleton(Client::class, function ($app) { $client = new Client(new Version1X('http://localhost:3000')); return $client; }); } ``` 此代码将在 Laravel 应用程序中注册 `Client` 类的单例实例。 4. 创建推送事件 在 Laravel 项目中,你需要创建推送事件。运行以下命令来创建: ``` php artisan make:event PushNotification ``` 在 `PushNotification` 类中,你需要添加以下内容: ```php public $message; public function __construct($message) { $this->message = $message; } public function broadcastOn() { return new PrivateChannel('push-notification'); } ``` 此代码将创建一个名为 `PushNotification` 的事件类,并在构造函数中接受要推送的消息。`broadcastOn` 方法指定了事件应该广播到的频道。 5. 创建事件监听器 在 Laravel 项目中,你需要创建事件监听器。运行以下命令来创建: ``` php artisan make:listener PushNotificationListener ``` 在 `PushNotificationListener` 类中,你需要添加以下内容: ```php use ElephantIO\Client; protected $socket; public function __construct(Client $socket) { $this->socket = $socket; } public function handle(PushNotification $event) { $this->socket->initialize(); $this->socket->emit('push-notification', ['message' => $event->message]); $this->socket->close(); } ``` 此代码将创建一个名为 `PushNotificationListener` 的事件监听器,并在构造函数中注入 `Client` 实例。`handle` 方法将使用 `Client` 类向 Socket.io 服务器发送消息。 6. 注册事件和监听器 在 Laravel 项目中,你需要在 `EventServiceProvider` 类中注册事件和监听器。在 `EventServiceProvider` 类中,你需要添加以下内容: ```php protected $listen = [ PushNotification::class => [ PushNotificationListener::class, ], ]; ``` 此代码将注册 `PushNotification` 事件和 `PushNotificationListener` 监听器。 7. 发送推送消息 在 Laravel 项目中,你可以使用以下代码发送推送消息: ```php event(new PushNotification('Hello world!')); ``` 这将触发 `PushNotification` 事件,并将消息发送到 Socket.io 服务器。然后,`PushNotificationListener` 监听器将从 Socket.io 服务器接收到消息并发送到连接到服务器的所有客户端。 以上是整合 PHPSocket.io 实现 web 消息推送的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值