swoole多人群聊 (php)
上代码
服务器端
<?php
//服务器代码
//创建 websocket 服务器代码
$ws = new swoole_websocket_server("0.0.0.0",9500);
// on 函数 open message close
$ws->on('open',function($ws,$request){
echo "新用户 $request->fd 加入。\n";
$GLOBALS['fd'][$request->fd]['id']=$request->fd;//设置用户ID
$GLOBALS['fd'][$request->fd]['name']='匿名用户';//设置用户名
});
//message
$ws->on('Message',function($ws,$frame){
$msg=$GLOBALS['fd'][$frame->fd]['name'].":".$frame->data."\n";
if(strstr($frame->data,"#name#")){
//用户昵称设置
$GLOBALS['fd'][$frame->fd]['name'