# sevent
https://github.com/melbahja/Sevent
// 监听
public function sevent($type = 'session_list', $param = '')
{
if (!$this->request->isAjax()) {
$this->redirect('User/Message/session');
}
$event = new \Common\Util\Sevent();
$ret = $event->response(function () use ($event, $type, $param) {
switch ($type) {
case 'session_list':
$session = $this->session();
$event->session_list(json_encode($session, JSON_NUMERIC_CHECK));
break;
case 'session_view':
parse_str($param);
$last_id = I('param.last_id');
$session = $this->get_new($uid, 1, $last_id);
$event->session_view(json_encode($session, JSON_NUMERIC_CHECK));
break;
case 'info_unread':
$map['status'] = 1;
$map['to_uid'] = $this->admin_uid;
$map['is_read'] = 0;
$map['type'] = 1;
$unread = M('user_message')->where($map)->count();
$event->info_unread(json_encode(['unread' => $unread], JSON_NUMERIC_CHECK));
break;
default:
break;
}
// conditions and code here
// $event->message('hello world'.$type); // sent a response message
});
return Response::create($ret, '', 200, $event->eventHeaders);
}
## 前端
<script type="text/javascript">
$(document).ready(function() {
$.sevent.init({
url: '{:U('sevent', ['ajax'=>1,'param'=>'', 'type'=>'session_list'])}',
});
$.sevent.on('open', function(event) {
// console.log('open');
});
$.sevent.on('session_list', function(response) {
response_list = $.sevent.json(response);
var tmpl = $('#main_template').html();
var compiledTemplate = $.Template7.compile(tmpl);
context = {
list:response_list
};
var give_win_html = compiledTemplate(context);
$('#main').html(give_win_html);
imgHeight();
// $.sevent.exit();
});
});
</script>