Git Product home page Git Product logo

easypushserver's Introduction

easyPushServer

基于bottle和gevent-websocket的简单消息推送服务器

工作原理

通过HTTP的POST或者GET方式向消息推送服务器发送消息,消息包括主题(topic)和数据(data)两部分。然后客户端通过websocket协议连接消息推送服务器并订阅对应主题。当有数据发送到推送服务器时,推送服务器通过websocket推送至客户端,由此实现数据的实时推送功能。

server.ini文件中可以定义推送服务器的应用名称,端口号,推送密码,服务器类型等,示例如下:

[server]
#应用名称
name=myapp
#服务器端口号
port=8080
#推送密码
password=123456
# 服务器类型,'gevent'或者'wsgi',默认使用'gevent'
#type=gevent

使用方式

  1. 运行 easyPushServer.py文件
  2. 客户端订阅主题,以浏览器为例,示例代码:
<!DOCTYPE html>
<html>
<head>
  <script src="topic.js"></script>
</head>
<body>
  <script type="text/javascript">
    //连接推送服务器
    var ws = new WebSocket("ws://127.0.0.1:8080/myapp");
    // 订阅名称为'testTopic'的主题
    ws.subscribe('testTopic', function(data){
      alert(data);
    });
    // 取消订阅名称为'testTopic'的主题
    // ws.unsubscribe('testTopic');
  </script>
</body>
</html>

注意,需要引入topic.js文件,该文件为浏览器WebSocket原型添加了主题订阅和取消订阅功能

  1. 推送消息到推送服务器,以python程序为例:
from urllib import request, parse

data = {'topic': 'testTopic', 'data': 'hello world!'}
r = request.urlopen(url='http://127.0.0.1:8080/myapp/publish', data=parse.urlencode(data).encode('utf-8'))
print(r.read().decode('utf-8'))

运行之后,控制台打印出推送返回信息:

{"code": 200, "data": "success!"}

此时浏览器界面弹出alert对话框推送结果表明推送成功。


本推送服务器支持多主题,多服务端同时推送消息,内部采用阻塞队列处理。可应用于需要实时推送数据的场景。

easypushserver's People

Contributors

lixk avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.