文档目录
[TOC]php
该程序对于php环境的要求是:css
可使用 curl -Ss http://www.workerman.net/chec... | php 来检测当前环境是否符合要求。若是不符合,须要根据提示安装必要的扩展。html
环境检测知足后,以ubuntu配置为例来进行配置:前端
$ sudo apt-get install php5-cli git gcc php-pear php5-dev libevent-dev -ylinux
$ pecl install eventgit
当出现Include libevent OpenSSL support [yes] :时,输入nogithub
$ sudo suweb
$ echo extension=event.so > /etc/php5/cli/conf.d/event.iniajax
$ su nancyjson
$ cd /var/www
$ git clone https://github.com/walkor/web...
// 下载composer
$ curl -sS https://getcomposer.org/insta... | php
// 设置全局
$ sudo mv composer.phar /usr/local/bin/composer
// 查看是否安装成功,若是有版本信息显示,则说明安装成功
$ composer -v
// 更新一下
$ composer self-update
// 进入到 web-msg-sender 项目中,使用composer进行安装
$ cd /var/www/web-msg-sender/
==$ composer install==
进入该项目文件,启动服务(以守护进程方式)
$ php start.php start -d
中止服务
$ php start.php stop
服务状态
$ php start.php status
客户端(即咱们的前端代码)使用 socket.io 插件经过websocket链接 服务器监听程序。
流程以下:
相关代码以下:
// 引入前端文件 <script src='//cdn.bootcss.com/socket.io/1.3.7/socket.io.js'></script> <script> /*加载提示*/ function loadTip(hit) { $.ajax({ url: "{{ url('/message/get_load') }}", type: 'get', success: function(msg) { if (msg.hit == 1 && hit==1) //播放消息提示音 $(".tip-mp3")[0].play(); if (msg.num > 0) { //显示未读消息数 $(".message-tip").show(500); $(".message-tip").html(msg.num); } else $(".message-tip").hide(500); } }); } // 初始化io对象 var socket = io('http://your.workerman.com:2120'); // uid 能够为网站用户的uid,做为例子这里用session_id代替 var uid = '{{ Auth::user()->id }} '; // 当socket链接后发送登陆请求 socket.on('connect', function(){socket.emit('login', uid);}); // 当服务端推送来消息时触发,查询后端程序 socket.on('new_msg', function(msg){loadTip(1);}); // 服务器端监听程序推送来在线数据时 socket.on('update_online_count', function(online_stat){ $("title").html("ERP "+online_stat); }); </script>
后端使用了Laravel第三方插件Notifynder 管理通知。它提供了一个完整的API来处理通知,例如存储,检索和组织代码库以处理数百个通知。
"fenos/notifynder": "^4.0"
Fenos\Notifynder\NotifynderServiceProvider::class,
在 aliases 数组中增长
'Notifynder' => Fenos\Notifynder\Facades\Notifynder::class,
$ php artisan vendor:publish --provider="FenosNotifynderNotifynderServiceProvider"
$ php artisan migrate
use Fenos\Notifynder\Notifable; class Erp_company_user extends Model implements AuthenticatableContract, CanResetPasswordContract { use Notifable; }
这时,咱们只要使用 Erp_company_user model实例,就能够调用 FenosNotifynderNotifable 中的方法。例如:
$user = Erp_company_user::first(); $notifications = $user->notifications;
'model' => 'App\Erp_company_user',
其余的根据实际应用时更改配置。
将要推送的信息和推送的人等相关数据组装好,使用curl远程访问 服务器监听程序,监听程序进行推送。
namespace App\Service\Setting; use App\Service\CommonService; class MessageService extends CommonService{ // 指明给谁推送,为空表示向全部在线用户推送 private $to_uid; // 推送的url地址,上线时改为本身的服务器地址 private $push_api_url = 'http://your.workerman.com:2121/'; protected function set_url($push_api_url){ $this->push_api_url=$push_api_url; } /** * 站内信推送 * @param int to_uid * @return array */ public function sent_message($to_uid=''){ $this->to_uid=$to_uid; $post_data = array( 'type' => 'publish', 'content' => 'You have a new message', 'to' => $this->to_uid, ); $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $this->push_api_url ); curl_setopt ( $ch, CURLOPT_POST, 1 ); curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data ); $return = curl_exec ( $ch ); curl_close ( $ch ); return $return; } }
客户端 向 后端程序请求发送站内信。
后端程序 将站内信信息保存到 Notification_category 数据表中,将要指定要推送的人信息保存到 notifications 表中。
调用 上述的 MessageService 服务将信息推送到 服务器监听程序。
相关代码以下:
// 保存站内信信息 $Notification_categorie = new Notification_category; $Notification_categorie->name = $name; $Notification_categorie->text = $text; $Notification_categorie->save(); //站内信id $this->categorie_id=$Notification_categorie->id; $this->categorie_num=0; try { //推送的人,这里以发送全体为例 $users = Erp_company_user::all(); //循环保存要通知的人站内信信息 Notifynder::loop($users, function(NotifynderBuilder $builder, $user, $key) { $this->categorie_num=$key+1; $builder->category($this->categorie_id) ->from($this->user['id']) ->to($user->id); })->send(); } catch (EntityNotIterableException $e) { } catch (IterableIsEmptyException $e) { } //推送到服务器端监听程序 $sent_message = $this->MessageService->sent_message();
查询5分钟内的站内信,是否有发送给本身的未读信息,有的话,返回未读信息数。
相关代码以下:
//未读站内信的数量 $not_read_num=$this->user->countNotificationsNotRead(); //是否提示新信息,看最新的站内信的时间是否在5分钟内 $message=$this->user->getLastNotification(); if(empty($message)) return array('num'=>0,'hit'=>0); $message_time=$message->updated_at; $five_minute_ago= Carbon::parse('-5 minute'); ($message_time->gt($five_minute_ago) && $message->read==0 )? $hit=1 : $hit=0; $result_array=array('num'=>$not_read_num,'hit'=>$hit); return $result_array;