Created
October 4, 2015 19:17
-
-
Save litnimax/39063715880a9b5767b9 to your computer and use it in GitHub Desktop.
litnimax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import math | |
| import os | |
| import time | |
| import webbrowser | |
| import zmq.green as zmq | |
| import gevent | |
| #from geventwebsocket import WebSocketApplication | |
| from geventwebsocket.handler import WebSocketHandler | |
| from paste.urlparser import StaticURLParser | |
| def zmq_producer(context): | |
| '''Produce a nice time series sine wave''' | |
| socket = context.socket(zmq.PUB) | |
| socket.connect('tcp://127.0.0.1:5000') | |
| while True: | |
| x = time.time() | |
| y = 2.5 * (1 + math.sin(x / 500)) | |
| socket.send(json.dumps(dict(x=x, y=y))) | |
| gevent.sleep(1) | |
| def zmq_server(context): | |
| '''Funnel messages coming from the external tcp socket to an inproc socket''' | |
| sock_incoming = context.socket(zmq.SUB) | |
| sock_outgoing = context.socket(zmq.PUB) | |
| sock_incoming.bind('tcp://*:5000') | |
| sock_outgoing.bind('inproc://queue') | |
| sock_incoming.setsockopt(zmq.SUBSCRIBE, "") | |
| while True: | |
| msg = sock_incoming.recv() | |
| sock_outgoing.send(msg) | |
| class WebSocketApp(object): | |
| '''Funnel messages coming from an inproc zmq socket to the websocket''' | |
| def __init__(self, context): | |
| self.context = context | |
| def __call__(self, environ, start_response): | |
| ws = environ['wsgi.websocket'] | |
| sock = self.context.socket(zmq.SUB) | |
| sock.setsockopt(zmq.SUBSCRIBE, "{") | |
| sock.connect('tcp://192.168.2.102:40968') | |
| #sock.setsockopt(zmq.SUBSCRIBE, "") | |
| #sock.connect('inproc://queue') | |
| while True: | |
| msg = json.loads(sock.recv()) | |
| if msg.get('Event') in ['Newchannel', 'Hangup']: | |
| ws.send(json.dumps(msg)) | |
| def main(): | |
| '''Set up zmq context and greenlets for all the servers, then launch the web | |
| browser and run the data producer''' | |
| context = zmq.Context() | |
| # zeromq: tcp to inproc gateway | |
| gevent.spawn(zmq_server, context) | |
| # websocket server: copies inproc zmq messages to websocket | |
| ws_server = gevent.pywsgi.WSGIServer( | |
| ('', 9999), WebSocketApp(context), | |
| handler_class=WebSocketHandler) | |
| # http server: serves up static files | |
| http_server = gevent.pywsgi.WSGIServer( | |
| ('', 8000), | |
| StaticURLParser(os.path.join(os.path.dirname(__file__),'htdocs'))) | |
| # Start the server greenlets | |
| http_server.start() | |
| #ws_server.start() | |
| ws_server.serve_forever() | |
| # Open a couple of webbrowsers | |
| # webbrowser.open('http://localhost:8000/index.html') | |
| # Kick off the producer | |
| #zmq_producer(context) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment