# notice **Repository Path**: hteen/notice ## Basic Information - **Project Name**: notice - **Description**: websocket通知配合yar简单实现 - **Primary Language**: PHP - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2016-11-10 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # websocket通知配合yar简单实现 ### 简单说明 就做了一件事情,使用[Yar](https://github.com/laruence/yar)的方式发送通知到websocket server,及时反馈到页面上 * 使用docker快捷部署服务 * 不用在已有项目中增加websocket server * 仅仅需要两步实现通知发送和接收 ### 部署并启动server 这里用到了[docker-compose](https://docs.docker.com/compose/overview/) ```shell git clone https://git.oschina.net/hteen/notice.git cd notice docker-compose up -d ``` ### JS客户端 ```javascript var wsServer = 'ws://127.0.0.1:9527'; var websocket = new WebSocket(wsServer); websocket.onopen = function (evt) { // {"action":"login","token":"xxxx"} websocket.send(''); }; websocket.onclose = function (evt) { console.log('close'); }; websocket.onmessage = function (evt) { var json = $.parseJSON(evt.data); // 转交函数处理每个通知事件 if (json.type) { var fuc = eval(json.type); fuc.apply(this,json.args); } console.log(json); }; websocket.onerror = function (evt, e) { console.log(evt); console.log(e); }; /** * 新增订单处理函数 * @param order_id */ function newRechargeOrder( order_id,msg ){ console.log(order_id); } ``` ### Yar发送通知 这里用的是Yar异步发送 ```php Yar_Concurrent_Client::call("http://127.0.0.1:8084/rpc/notice", "newRechargeOrder", array("123")); Yar_Concurrent_Client::loop(function($retval, $callinfo){return true;}); ``` ### 需要注意的地方 * 通知处理的方法写在 `rpc/application/models`目录下 * 通知的方法名就是JS处理通知事件的方法名称