PHP code example of beansir / newx-server

1. Go to this page and download the library: Download beansir/newx-server library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

beansir / newx-server example snippets


#!/usr/bin/env php

defined('PROJECT_PATH') or define('PROJECT_PATH', __DIR__);
 


return [
    'app' => [
        // 服务器配置
        'tcp' => [
            'host' => '0.0.0.0',
            'port' => 9501
        ],
        'web-socket' => [
            'host' => '0.0.0.0',
            'port' => 9502
        ],
        'http' => [
            'host' => '0.0.0.0',
            'port' => 9503
        ],
    ],
    'database' => [
        // 数据库配置,非必须配置项
        'default' => [
            'host'      => '127.0.0.1',
            'user'      => 'user',
            'password'  => 'password',
            'db'        => 'db',
            'type'      => 'mysqli'
        ],
    ]
];


namespace service;
class WebSocket extends \newx\server\base\WebSocket
{
    /**
     * 监听客户端连接
     * @param $server
     * @param $request
     */
    public function open($server, $request)
    {
        var_dump($request);
    }

    /**
     * 监听客户端数据
     * @param $server
     * @param $frame
     */
    public function message($server, $frame)
    {
        var_dump($frame);
    }

    /**
     * 监听客户端关闭连接
     * @param $server
     * @param $fd
     */
    public function close($server, $fd)
    {
        var_dump($fd);
    }
}