定制 kosar501/phpwebsocket 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

kosar501/phpwebsocket

Composer 安装命令:

composer require kosar501/phpwebsocket

包简介

a simple package interaction websocket server and client

README 文档

README

Overview

Real-time messaging platform utilizing WebSocket for bidirectional communication between clients and a ZeroMQ-powered backend for scalable, asynchronous message distribution. This project enables efficient and low-latency communication across multiple connected clients, with ZeroMQ handling high-throughput messaging and WebSocket ensuring real-time updates in a responsive web interface.

WebSocket Server: Listens for WebSocket connections and broadcasts messages to connected clients.

Installation

Step 1: Clone the Repository

Clone the repository to your local machine:

git clone https://github.com/kosar501/PhpWebSocket.git
cd your-project-folder

Step 2: Install Dependencies

composer install

Step 2: Install Dependencies

composer install

Running the Server

There are two main components in the system: the WebSocket server and the Redis queue consumer. You can run both components as separate processes, and they will be managed using Supervisor.

1: Running WebSocket Server Along With ZMQ

1.1: Manually (for development or testing)

php server.php

1.2: Using Supervisor (Recommended for production)

[program:websocket-server]
command=php /path/to/your/project/server.php
autostart=true
autorestart=true
stderr_logfile=/var/log/websocket_server.err.log
stdout_logfile=/var/log/websocket_server.out.log

After adding this configuration, update Supervisor:

supervisorctl start websocket-server

Start the WebSocket server:

supervisorctl start websocket-server

If you encounter issues, restart the processes via Supervisor:

supervisorctl restart websocket-server

How to Use

You can use the Client class to send messages to the WebSocket server through Redis. This class sends messages to the Redis queue that the consumer will process.

Server Side:

Example:

    $client = new MessagePublisher();

    // Prepare the message as an associative array
    $message = json_encode(['topic' => 'news', 'content' => 'This is a test message']);
    $client->sendMessage($message);

On the Client Side (Web Browser):

Example:

You can check examples folder

const socket = new WebSocket('ws://127.0.0.1:5555');

socket.onmessage = function(event) {
    const message = JSON.parse(event.data);
    console.log('Received message:', message);
    // Now you can access the message properties like message.action, message.username, etc.
};

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-12