1 Star 0 Fork 0

会PS的小码农 / phpsocketio

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载

A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.

Permissions
  • Commercial use
  • Modification
  • Distribution
  • Private use
Limitations
  • Liability
  • Warranty
Conditions
  • License and copyright notice
README.md 4.17 KB
一键复制 编辑 原始数据 按行查看 历史
walkor 提交于 2020-03-20 18:42 . Update README.md

phpsocket.io

A server side alternative implementation of socket.io in PHP based on Workerman.

Notice

Only support socket.io v1.3.0 or greater.
This project is just translate socket.io by workerman.
More api just see http://socket.io/docs/server-api/

Install

composer require workerman/phpsocket.io

Examples

Simple chat

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();

Another chat demo

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();

Enable SSL for https

(phpsocket.io>=1.1.1 && workerman>=3.3.7 required)

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();

手册

中文手册

Livedemo

chat demo

Run chat example

cd examples/chat

Start

php start.php start for debug mode

php start.php start -d for daemon mode

Stop

php start.php stop

Status

php start.php status

License

MIT

1
https://gitee.com/liushuai05/phpsocketio.git
git@gitee.com:liushuai05/phpsocketio.git
liushuai05
phpsocketio
phpsocketio
master

搜索帮助