基于Nodejs的高并发实时消息转发系统 message pusher and written in nodejs based on socket.io and express
来源:互联网 发布:软件众包平台 编辑:程序博客网 时间:2024/05/16 04:09
基于Nodejs的消息转发系统 message pusher and written in nodejs based on socket.io and express
github地址:https://github.com/gytai/node-websocket-msg-sender
消息实时推送,支持在线用户数实时统计。基于Socket.IO开发,使用websocket推送数据,当浏览器不支持websocket时自动切换comet推送数据。
支持Linux,mac,windows等环境部署。
效果截图
线上demo
http://112.74.81.224:3000/
可以通过url:http://112.74.81.224:3000/sendMsg/?type=private&uid=1504936989000&content=消息内容 向当前用户发送消息
可以通过url:http://112.74.81.224:3000/sendMsg/?type=public&content=消息内容 向所有在线用户推送消息
uid为接收消息的uid,如果不传递则向所有人推送消息
content 为消息内容
注:可以通过php或者其它语言的curl功能实现后台推送
下载安装
1、git clone https://github.com/gytai/node-websocket-msg-sender.git
2、npm install
3、apt-get install redis-server
4、redis-server
后端服务启动停止,先安装PM2(Advanced Node.js process manager,http://pm2.keymetrics.io/)
启动服务
pm2 start bin/www –name msg-sender
停止服务
pm2 stop msg-sender
Web前端代码类似:
// 引入前端文件<script src="/socket.io/socket.io.js"></script><script> var socket = io.connect('http://localhost:3000'); socket.emit('login', new Date().getTime()); // 后端推送来消息时 socket.on('message', function(msg){ $('#content').html('收到消息:'+msg); $('.notification.sticky').notify(); }); // 后端推送来在线数据时 socket.on('update_online_count', function(data){ console.log(data); $('#online_box').html('当前在线客户端数: '+data.online_count); });</script>
其他客户端
根据websocket协议即可。具体参考websocket协议。
Nodejs后端调用api向任意用户推送数据
var type = req.query.type || msgType.public; var content = req.query.content || 'none'; var uid = req.query.uid; switch (type){ case msgType.public: ioSvc.serverBroadcastMsg(content); break; case msgType.private: if(!uid){ return res.send({code:400,msg:'uid参数必传'}); } ioSvc.serverToPrivateMsg(uid,content); break; }
Http 发送数据,可以配置跨站发送(需要设置跨域放行)。例如安卓或者IOS等其他客户端也可以方便的发送消息。
可以通过url:http://localhost:3000/sendMsg/?type=private&uid=1504936989000&content=消息内容 向当前用户发送消息
可以通过url:http://localhost:3000/sendMsg/?type=public&content=消息内容 向所有在线用户推送消息
备注
php 版本可以参考:workman的web-msg-sender
- 基于Nodejs的高并发实时消息转发系统 message pusher and written in nodejs based on socket.io and express
- socket.io (nodejs+express)
- NodeJs内存session: Express v4.x and Socket.io 1.3.x 用(express-session)共享session
- NodeJs upload files based on Express
- 基于nodejs+socket.io的chatrobot
- NodeJS+Express+Socket.io的简单及时通讯
- nodejs express+socket.io多线程原始实现
- Java and Nodejs on AES
- socket.io 实时通信在nodejs下的应用
- nodejs+socket.io+zmq 实现实时刷新
- 基于nodejs+express+ejs的系统的开发环境搭建
- DoraCMS 基于Nodejs+express+mongodb 内容管理系统
- 基于Vue、Nodejs、Socket.io的聊天应用
- nodejs 基于socket.io实现聊天室
- Installing nodejs and npm on CentOS
- windows下安装nodejs、 express +、socket.io和redis
- Ubuntu下配置Nodejs,并安装express和socket.io
- Ubuntu下配置Nodejs,并安装express和socket.io
- 从零开始ElasticSearch
- 我的Markdown的利器——Markdown Here、有道云笔记、iPic
- 2017.09.09 Caffe in Windows
- 前端知识点总结(js)
- struts2 实现简单的文件上传、下载功能
- 基于Nodejs的高并发实时消息转发系统 message pusher and written in nodejs based on socket.io and express
- socket编程 及select poll epoll示例
- throws 和throw有什么区别?
- shiro1.4.0是不是有bug啊
- c++初级 之 模板
- bzoj1011 [HNOI2008]遥远的行星
- python魔法方法
- 《统计学习方法》笔记一
- bzoj1029 [JSOI2007]建筑抢修