PHP code example of jimmyfen / websocket-server

1. Go to this page and download the library: Download jimmyfen/websocket-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/ */

    

jimmyfen / websocket-server example snippets


    array(
        'HOST' => '0.0.0.0',  // 绑定监听域名
        'PORT' => 9501,       // 绑定监听端口
        'SETTING' => array(
            ...               // 此处为swoole设置参数,详情参考swoole文档
        )
    )

// 文件index.php


 $request){
    // do something
}

function onMessage($server, $frame){
    // do something
}

function onClose($server, $fd){
    // do something
}

function onRequest($request, $response){
    // do something
}

// 监听事件,参考swoole文档(函数可以是对象对应的方法,参考call_user_func_array()函数)
$callable = [
    'open' => 'onOpen',
    'message' => 'onMessage',
    'close' => 'onClose',
    'request' => 'onRequest'
];
// 配置项,SETTING部分参考swoole文档
$config = [
    'HOST' => '0.0.0.0',
    'PORT' => 9501,
    'SETTING' => [
        'max_request' => 1000,
        'work_num' => 2,
        'pid_file' => './server.pid',
        'log_file' => './console.log'
    ]
];

\Websocket\App::run($callable, $config);