workerman怎么運行

workerman怎么運行

首先下載workerman??https://www.workerman.net/download ? ? ? ? ? ? ? ? ? ? ? ?(推薦學習: workerman教程

workerman怎么運行下載好后在workerman文件下創建一個新的文件start.php

workerman怎么運行

代碼如下

<?php use WorkermanWorker;   //Autoloader.php路徑 require_once "./Autoloader.php";   $global_uid = 0;   // 當客戶端連上來時分配uid,并保存連接,并通知所有客戶端 function handle_connection($connection) {     global $text_worker, $global_uid;     // 為這個連接分配一個uid     $connection->uid?=?++$global_uid; } ? //?當客戶端發送消息過來時,轉發給所有人 function?handle_message($connection,?$data) { ????global?$text_worker; ????foreach($text_worker-&gt;connections?as?$conn) ????{ ????????$conn-&gt;send("user[{$connection-&gt;uid}]?said:?$data"); ????} } ? //?當客戶端斷開時,廣播給所有客戶端 function?handle_close($connection) { ????global?$text_worker; ????foreach($text_worker-&gt;connections?as?$conn) ????{ ????????$conn-&gt;send("user[{$connection-&gt;uid}]?logout"); ????} } ? //?創建一個文本協議的Worker監聽2000接口??用0.0.0.0方便鏈接內網外網 $text_worker?=?new?Worker("websocket://0.0.0.0:2000");?? ? //?只啟動1個進程,這樣方便客戶端之間傳輸數據 $text_worker-&gt;count?=?1; ? $text_worker-&gt;onConnect?=?'handle_connection'; $text_worker-&gt;onMessage?=?'handle_message'; $text_worker-&gt;onClose?=?'handle_close'; ? Worker::runAll();

然后命令行運行? ?php start.php start

workerman怎么運行

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享