PHP code example of ller / chat
1. Go to this page and download the library: Download ller/chat library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ller / chat example snippets
use Ller\Chat\Server;
set_time_limit(0);
date_default_timezone_set('Asia/shanghai');
$ws = new Server();
// 发送成功,发送成功会把内容以及自己和对方的唯一标识传到这个方法,可以用来记录聊天记录。
$ws->sent_success(function($send) {
/*{"to":1,"type":"user","from":2,"content":"hello,world","create_date":"01-01 08:00:00","is_send":true}*/
file_put_contents('./success.log', json_encode($send)."\r\n", FILE_APPEND);
});
// 发送失败,对方不在线的时候会发送失败,此时可以把数据保存起来和后面的等待发送配合使用
$ws->sent_fail(function($send) {
file_put_contents('./fail.log', json_encode($send)."\r\n", FILE_APPEND);
});
// 等待发送,可以在某个用户登录的时候根据这个用户查询他的未读信息。
// $send ---->["to"=>2, "room"=>"", "type"=>"login", "content"=>'聊天内容', "from"=>1]
$ws->wait_send(function($send) {
$data = [
[
'to'=>1,
'from'=>2,
'content'=>'hello,world',
'create_date'=>time()
],
[
'to'=>1,
'from'=>2,
'content'=>'world,hello',
'create_date'=>time()
]
];
return $data;
});
$ws->run("0.0.0.0", "8080");