1 Star 1 Fork 1

大吉 / web-msg-sender

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

web-msg-sender

Web消息实时推送,支持在线用户数实时统计。基于PHPSocket.IO开发,使用websocket推送数据,当浏览器不支持websocket时自动切换comet推送数据。

效果截图

web-msg-sender-demo

线上demo

接收消息页面:http://www.workerman.net:2123/
后端推送接口url:http://www.workerman.net:2121/?type=publish&to=&content=msgcontent
to为接收消息的uid,如果不传递则向所有人推送消息
content 为消息内容

注:可以通过php或者其它语言的curl功能实现后台推送

下载安装

1、git clone https://github.com/walkor/web-msg-sender

2、composer install

后端服务启动停止

Linux系统

启动服务

php start.php start -d

停止服务

php start.php stop

服务状态

php start.php status

windows系统

双击start_for_win.bat

如果启动不成功请参考 Workerman手册 配置环境

前端代码类似:

// 引入前端文件
<script src='//cdn.bootcss.com/socket.io/1.3.7/socket.io.js'></script>
<script>
// 初始化io对象
var socket = io('http://'+document.domain+':2120');
// uid 可以为网站用户的uid,作为例子这里用session_id代替
var uid = '<?php echo session_id();?>';
// 当socket连接后发送登录请求
socket.on('connect', function(){socket.emit('login', uid);});
// 当服务端推送来消息时触发,这里简单的aler出来,用户可做成自己的展示效果
socket.on('new_msg', function(msg){alert(msg);});
</script>

后端调用api向任意用户推送数据

<?php
// 指明给谁推送,为空表示向所有在线用户推送
$to_uid = '';
// 推送的url地址,上线时改成自己的服务器地址
$push_api_url = "http://workerman.net:2121/";
$post_data = array(
   'type' => 'publish',
   'content' => '这个是推送的测试数据',
   'to' => $to_uid, 
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $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 );
var_export($return);

常见问题:

如果通信不成功检查防火墙
/sbin/iptables -I INPUT -p tcp --dport 2120 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 2121 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 2123 -j ACCEPT

workerman相关参见 www.workerman.net

workerman更多有趣的应用:

小蝌蚪聊天室

多人在线flappy birds

其它

The MIT License Copyright (c) 2009-2015 walkor<walkor@workerman.net> and contributors (see https://github.com/walkor/workerman/contributors) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Web message pusher written in PHP based on workerman. 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/daji666/web-msg-sender.git
git@gitee.com:daji666/web-msg-sender.git
daji666
web-msg-sender
web-msg-sender
master

搜索帮助