写了个聊天室,只用到了workerman,
想在来消息时候进行全员广播,发现只有某一个客户端收的到信息
$worker->onMessage = function($conn,$data) use ($worker,$mongo){
if(!$data)
{
return;
}
$json_arr = BaseJson::decode($data);
foreach($worker->connections as $connection){
switch($json_arr){
case 'pong':
case 'login':
// $msg = $mongo->find('chatmsg', );
// $json = ;
// $connection->send(BaseJson::encode($json));
return;
case 'say':
$content = $json_arr;
$time = date('Y-m-d H:i:s');
$json = ;
$mongo->insert('chatmsg', $json);
$connection->send(BaseJson::encode($json));
return;
}
}
};
前台
ws.onmessage = function(evt)
{
var obj = (JSON.parse(evt.data));
if(obj.type=='statistics'){
$('#people_num').html(obj.total_conn_person);
}else if(obj.type=='showsay'){
$('#ct').append(
'<div>"'+obj.content+'" "'+obj.time+'"</div>'
);
console.log(obj);
}
};
case ‘say’:
分支里面不要写return
所跌死捏。。。又犯傻了,刚把TcpConnection看了遍。谢谢大大
不客气