workerman/phpsocket.io

基于 Workerman 的 PHP 实现 socket.io 的服务器端替代方案

资助包维护!
Open Collective
Patreon

安装数量: 463 785

依赖项: 27

建议者: 0

安全性: 0

星标: 2 288

关注者: 121

分支: 509

开放问题: 60

v2.1.0 2024-04-08 17:20 UTC

README

基于 socket.ioWorkerman 的 PHP 实现的服务器端 socket.io 替代方案。

注意

仅支持 socket.io >= v1.3.0 且 <= v2.x
本项目仅通过 workerman 转译 socket.io。
更多 API 请参考 https://socketio.node.org.cn/docs/v2/server-api/

安装

composer require workerman/phpsocket.io

示例

简单聊天

start.php

use Workerman\Worker;
use PHPSocketIO\SocketIO;
require_once __DIR__ . '/vendor/autoload.php';

// Listen port 2021 for socket.io client
$io = new SocketIO(2021);
$io->on('connection', function ($socket) use ($io) {
    $socket->on('chat message', function ($msg) use ($io) {
        $io->emit('chat message', $msg);
    });
});

Worker::runAll();

另一个聊天示例

https://github.com/walkor/phpsocket.io/blob/master/examples/chat/start_io.php

use Workerman\Worker;
use PHPSocketIO\SocketIO;
require_once __DIR__ . '/vendor/autoload.php';

// Listen port 2020 for socket.io client
$io = new SocketIO(2020);
$io->on('connection', function ($socket) {
    $socket->addedUser = false;

    // When the client emits 'new message', this listens and executes
    $socket->on('new message', function ($data) use ($socket) {
        // We tell the client to execute 'new message'
        $socket->broadcast->emit('new message', array(
            'username' => $socket->username,
            'message' => $data
        ));
    });

    // When the client emits 'add user', this listens and executes
    $socket->on('add user', function ($username) use ($socket) {
        global $usernames, $numUsers;

        // We store the username in the socket session for this client
        $socket->username = $username;
        // Add the client's username to the global list
        $usernames[$username] = $username;
        ++$numUsers;

        $socket->addedUser = true;
        $socket->emit('login', array( 
            'numUsers' => $numUsers
        ));

        // echo globally (all clients) that a person has connected
        $socket->broadcast->emit('user joined', array(
            'username' => $socket->username,
            'numUsers' => $numUsers
        ));
    });

    // When the client emits 'typing', we broadcast it to others
    $socket->on('typing', function () use ($socket) {
        $socket->broadcast->emit('typing', array(
            'username' => $socket->username
        ));
    });

    // When the client emits 'stop typing', we broadcast it to others
    $socket->on('stop typing', function () use ($socket) {
        $socket->broadcast->emit('stop typing', array(
            'username' => $socket->username
        ));
    });

    // When the user disconnects, perform this
    $socket->on('disconnect', function () use ($socket) {
        global $usernames, $numUsers;

        // Remove the username from global usernames list
        if ($socket->addedUser) {
            unset($usernames[$socket->username]);
            --$numUsers;

            // echo globally that this client has left
            $socket->broadcast->emit('user left', array(
               'username' => $socket->username,
               'numUsers' => $numUsers
            ));
        }
   });
});

Worker::runAll();

启用 SSL 以使用 https

(phpsocket.io >= 1.1.1 且 workerman >= 3.3.7)

start.php

<?php

use Workerman\Worker;
use PHPSocketIO\SocketIO;

require_once __DIR__ . '/vendor/autoload.php';

// SSL context
$context = array(
    'ssl' => array(
        'local_cert'  => '/your/path/of/server.pem',
        'local_pk'    => '/your/path/of/server.key',
        'verify_peer' => false
    )
);
$io = new SocketIO(2021, $context);

$io->on('connection', function ($connection) use ($io) {
    echo "New connection coming\n";
});

Worker::runAll();

确认回调

use Workerman\Worker;
use PHPSocketIO\SocketIO;

require_once __DIR__ . '/vendor/autoload.php';

$io = new SocketIO(2021);

$io->on('connection', function ($connection) use ($io) {
    $socket->on('message with ack', function ($data, $callback) use ($socket, $io) {
        // acknowledgement callback
        if ($callback && is_callable($callback)) {
            $callback(0);
        }
    });
});

Worker::runAll();

手册

中文手册

在线演示

聊天示例

运行聊天示例

cd examples/chat

启动

php start.php start 用于调试模式

php start.php start -d 用于守护进程模式

停止

php start.php stop

状态

php start.php status

许可

MIT