中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php使用長連接

發布時間:2020-07-29 05:29:32 來源:網絡 閱讀:1061 作者:Lee_吉 欄目:web開發
  1. 安裝:
    composer  require  workerman/gateway-worker
  2. 服務端:
    <?php
    require_once "vendor/autoload.php";
    use Workerman\Worker;
    use GatewayWorker\Gateway as gate;
    use GatewayWorker\BusinessWorker;
    use GatewayWorker\Register;
    use GatewayWorker\Lib\Gateway;
    use Medoo\Medoo;
    $gateway = new gate("websocket://0.0.0.0:2345");
    $gateway->startPort = 30000;
    $gateway->count = 4;
    $gateway->name = 'gateway';
    $gateway->registerAddress = '127.0.0.1:1234';
    $business = new BusinessWorker();
    $business->count = 4;
    $business->registerAddress = '127.0.0.1:1234';
    $register = new Register('text://0.0.0.0:1234');
    $register->name = 'register';
    class Events{
    public static function onConnect($client_id){
        $arr = array(
                'remote_ip'=>$_SERVER['REMOTE_ADDR'],
                'remote_port'=>$_SERVER['REMOTE_PORT'],
                'gate_ip'=>$_SERVER['GATEWAY_ADDR'],
                'gate_port'=>$_SERVER['GATEWAY_PORT'],
                'client_id'=>$_SERVER['GATEWAY_CLIENT_ID']
            );
        $ret = json_encode($arr);
        return GateWay::sendToCurrentClient($ret);
    }
    public static function onMessage($client_id, $message){
        return GateWay::sendToCurrentClient($message);
    }
    public static function onClose($client_id){
       GateWay::sendToAll("client[$client_id] logout\n");
    }
    }
    Worker::runAll();
  3. 客戶端:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    <input type="text">
    <br>
    <input type="button" value="提交">
    <script>
        var ws = new WebSocket('ws://192.168.8.81:2345')
        ws.onopen = function(data) {
            console.log('連接開始\n')
        }
        ws.onmessage = function(data) {
            console.log('接收消息:'+data.data)
        }
        ws.onclose = function(data) {
            console.log('關閉連接\n')
        }
        ws.onerror = function(data) {
            console.log('遇到錯誤\n')
        }
        var text = document.getElementsByTagName('input')[0]
        var button = document.getElementsByTagName('input')[1]
        button.onclick = function(){
            var val = text.value
            ws.send(val)
        }
    </script>
    </body>
    </html>
  4. 測試:
    php使用長連接
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

大悟县| 四会市| 彭阳县| 双鸭山市| 友谊县| 永平县| 宁河县| 宁蒗| 饶平县| 青河县| 上饶县| 基隆市| 阳谷县| 彝良县| 扶风县| 阜城县| 祁门县| 湘潭市| 甘孜县| 泾阳县| 云浮市| 泌阳县| 谷城县| 尤溪县| 兴国县| 舒兰市| 阿瓦提县| 五峰| 文登市| 灵寿县| 望谟县| 湘潭市| 额济纳旗| 深水埗区| 进贤县| 陆良县| 金山区| 嵩明县| 和平区| 淳安县| 邵阳市|