多人聊天室。双人聊天室后面再加
作者:互联网
calls=array(
'id' => null,
'nid' => null,
'name' => '',
'cur_time' => '',
'context' => '',
'switch' => '',
);
//用户
$this->user=array(
'nid'=>null,
'name'=> '',
);
//注册
$this->us_login = array(
'name' => array('老王','锅巴','pp'),
);
}
public function edit(){
$this->us_login?$this->us_login:$this->user;
if(!$this->us_login) die('非法操作');
$coun=count($this->user['nid']);
if ($this->user['nid']==null) {
unset($this->user['nid'],$this->user['name']);
$users=array(
'nid'=> 0,
'name'=> $this->us_login['name'][0],
);
array_push($this->user,$users);
}
return $this->user;
}
//新增
public function add(){
$coun = count($this->user);
$coun2 = count($this->us_login['name']);
for ($i = 0; $i < $coun2; $i++) {
$users=array(
'nid'=> $i,
'name'=> $this->us_login['name'][$i],
);
array_push($this->user,$users);
unset($this->user[0]);
}
sort($this->user);
}
/**
* 传nid给方法验证用户
* 格式
*/
public function add_mes($name){ //pp
$user=$this->user;
//登录
for ($i = 0; $i < count($user); $i++) {
if($user[$i]['name']==$name){
$this->nid=$user[$i]['nid'];
$this->name=$user[$i]['name'];
$suss=1;
}
}
if ($suss !=1) {
exit('code:-1,msg:没有登录');
}
$token=md5(rand(1,100));
return $token;
}
//写数据
public function add_mes_wr($context,$name) {
$coun=count($this->calls['id']);
$coun+=1;
$calls['id'] ='';
$calls['nid'] = $this->nid;
$calls['name'] = $name;
$calls['cur_time'] = time();
$calls['context']= $context;
$calls['switch'] = '';
if ($coun) {
$calls['id']=$coun;
}
array_push($this->calls,$calls);
$c=$this->calls;
unset($c['id'],$c['nid'],$c['name']);
unset($c['cur_time'],$c['context'],$c['switch']);
return $c;
}
}
$call = new Call();
$data=$call->edit();
$call->add();
$name1='老王';
$name2='锅巴';
$token =$call->add_mes($name1);
$token1=$call->add_mes($name2);
if (!$token&&$token1) {
exit('code:-1,msg:未登录');
}
//开始聊天
$c=$call->add_mes_wr('你好',$name1);
sleep(3);
$c=$call->add_mes_wr('有人吗?',$name2);
//时间 name 内容
$kg=' ';
foreach ($c as $v) {
$date=date('Y/m/d h:i:s',$v['cur_time']);
echo $date.$kg.$v['name'].':'.$kg.$v['context']."\n";
}
标签:聊天室,name,calls,nid,add,user,多人,array,双人 来源: https://www.cnblogs.com/applystyle/p/14643069.html