oshitsd/php-socket 问题修复 & 功能扩展

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

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

oshitsd/php-socket

最新稳定版本:v1.0.4

Composer 安装命令:

composer require oshitsd/php-socket

包简介

A PHP package to interact with WebSocket servers. It provides methods to connect, send, receive, and close WebSocket connections, with Laravel integration for seamless usage.

README 文档

README

PhpSocket is a Laravel wrapper for handling Socket.IO WebSocket communication using a simple, expressive API. Built on top of the textalk/websocket client, it allows your Laravel app to communicate easily with a Socket.IO server.

🚀 Features

  • Connect to a Socket.IO WebSocket server
  • Send and receive messages
  • Built-in support for custom payloads and events
  • Laravel Facade for simple usage
  • Configurable host and port

🧰 Requirements

  • PHP 7.4 or higher
  • Laravel 8, 9, 10, 11, 12 or upper
  • WebSocket server with Socket.IO (EIO=4) support

📦 Installation

Install via Composer:

composer require oshitsd/php-socket

Publish the config file:

php artisan vendor:publish --tag=phpsocket-config

This will create a config/phpsocket.php file where you can configure the WebSocket host and port.

⚙️ Configuration

In your .env file, add:

PHP_SOCKET_ENV=production
PHP_SOCKET_HOST=socket.techcanvas.info
PHP_SOCKET_API_KEY=demo-tech-canvas-api-key

Or modify the config/phpsocket.php file directly.

🧪 Basic Usage

🔌 Connect to Socket

Without Authentication

use Oshitsd\PhpSocket\Facades\PhpSocket;

PhpSocket::connect();

With Authentication

use Oshitsd\PhpSocket\Facades\PhpSocket;

$connect = PhpSocket::connect([
    "room" => "DEMO_CHAT_ROOM" // Required: Name of the chat room to join
    "role" => "user", // Optional: User role, defaults to 'user'. Options: 'user' or 'agent'
    "userId" => 8001, // Optional: Unique user identifier
    "userName" => "OSHIT SUTRA DAR", // Optional: Display name of the user
]);

📤 Send a Message

$response = PhpSocket::send([
    "event" => "demo_chat", // Required: Name of the event to broadcast
    "to" => "all", // Target recipient(s). Options: 'all' (broadcast to everyone) or a specific user ID to send a private message.
    "message" => [
        "time" => date('Y-m-d H:i:s'),
        "text" => "Laravel says hi 👋",
        "user" => [
            "id" => 8001,
            "name" => "OSHIT SUTRA DAR"
        ]
    ]
]);

📩 Receive Message Response

$response = PhpSocket::receiveAck();

🔒 Close the Connection

$close = PhpSocket::close();

🛣️ Example Route Usage

You can quickly test sending a message using a simple route in your Laravel application.

use Illuminate\Support\Facades\Route;
use Oshitsd\PhpSocket\Facades\PhpSocket;

Route::get('send-notification', function () {

    $connect = PhpSocket::connect([
        "room" => "DEMO_CHAT_ROOM",
        "userId" => 8001,
        "userName" => "OSHIT SUTRA DAR",
    ]);

    $response = PhpSocket::send([
        "event" => "demo_chat",
        "to" => "all",
        "message" => [
            "time" => date('Y-m-d H:i:s'),
            "text" => "Laravel says hi 👋",
            "user" => [
                "id" => 8001,
                "name" => "OSHIT SUTRA DAR"
            ]
        ]
    ]);

    $close = PhpSocket::close();

    // Return the connection and message response
    return response()->json([
        'connect' => $connect,
        'response' => $response,
        'close' => $close
    ]);
});

When you visit http://your-app-url/send-notification, this route will:

  • Connect to the WebSocket server
  • Send a message to all connected clients
  • Return the connection and response details as a JSON response

🔍 Live Demo

Once you hit the route http://your-app-url/send-notification, the message will be broadcasted to all connected clients.

You can view the real-time message output here: 👉 Chat App Demo

🧼 Example Output

👋 Connected to socket server successfully.
📤 Message sent.
📨 Received: {message response}
🔒 Connection closed.

🧪 Testing

You can run the package tests with:

./vendor/bin/phpunit

Tests are located in the tests/ directory.

📄 License

This package is open-source software licensed under the MIT license.

👨‍💻 Credits

Developed by OSHIT SD
WebSocket client powered by textalk/websocket

Packagist Version

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-24