是否有任何教程或指南显示如何在PHP中编写一个简单的websockets服务器?我试过在谷歌上寻找它,但我找不到很多.我发现phpwebsockets但它现在已经过时,不支持最新的协议.我自己尝试更新它,但它似乎不起作用.
#!/php -q
<?php /* >php -q server.php */
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$master = WebSocket("localhost",12345);
$sockets = array($master);
$users = array();
$debug = false;
while(true){
$changed = $sockets;
socket_select($changed,$write=NULL,$except=NULL,NULL);
foreach($changed as $socket){
if($socket==$master){
$client=socket_accept($master);
if($client<0){ console("socket_accept() failed"); continue; }
else{ connect($client); }
}
else{
$bytes = @socket_recv($socket,$buffer,2048,0);
if($bytes==0){ disconnect($socket); }
else{
$user = getuserbysocket($socket);
if(!$user->handshake){ dohandshake($user,$buffer); }
else{ process($user,$buffer); }
}
}
}
}
//---------------------------------------------------------------
function process($user,$msg){
$action = unwrap($msg);
say("< ".$action);
switch($action){
case "hello" : send($user->socket,"hello human"); break;
case …Run Code Online (Sandbox Code Playgroud) 我有一个PHP脚本,只生成日志到客户端.
当我回应某些内容时,我希望它能够即时转移到客户端.
(因为在脚本处理过程中,页面是空白的)
,我已经打得四处ob_start()和ob_flush(),但他们没有工作.
什么是最好的解决方案?
PS:在echo通话结束时放一个同花牌有点脏......
编辑:Answers既不工作,PHP或Apache错误?