.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "GEVENT-WEBSOCKET" "1" "Nov 24, 2024" "0.10" "gevent-websocket" .SH NAME gevent-websocket \- gevent-websocket Documentation .sp gevent\-websocket is a \X'tty: link http://www.websocket.org/aboutwebsocket.html'\fI\%WebSocket\fP\X'tty: link' library for the \X'tty: link http://www.gevent.org'\fI\%gevent\fP\X'tty: link' networking library written written and maintained by \X'tty: link http://www.noppo.pro'\fI\%Jeffrey Gelens\fP\X'tty: link' It is licensed under the BSD license. .INDENT 0.0 .INDENT 3.5 .sp .EX from geventwebsocket import WebSocketServer, WebSocketApplication, Resource class EchoApplication(WebSocketApplication): def on_message(self, message): self.ws.send(message) WebSocketServer( (\(aq\(aq, 8000), Resource({\(aq/\(aq: EchoApplication}) ) .EE .UNINDENT .UNINDENT .sp It isn\(aqt necessary to use the build\-in \fIWebSocketServer\fP to start using WebSockets. WebSockers can be added to existing applications very easy by making the non\-standard \fIwsgi.websocket\fP variable available in the WSGI environment. An example using \X'tty: link http://flask.pocoo.org'\fI\%Flask\fP\X'tty: link' follows: .INDENT 0.0 .INDENT 3.5 .sp .EX from geventwebsocket import WebSocketServer, WebSocketError from flask import Flask, request, render_template app = Flask(__name__) @app.route(\(aq/\(aq) def index(): return render_template(\(aqindex.html\(aq) @app.route(\(aq/api\(aq) def api(): ws = request.environ.get(\(aqwsgi.websocket\(aq) if not ws: abort(400, \(dqExpected WebSocket request\(dq) while True: try: message = ws.receive() ws.send(\(dqYour message was: {}\(dq.format(message)) except WebSocketError: # Possibility to execute code when connection is closed break if __name__ == \(aq__main__\(aq: server = WebSocketServer((\(dq\(dq, 8000), app) server.serve_forever() .EE .UNINDENT .UNINDENT .sp Also the browser Javascript application can be very simple: .INDENT 0.0 .INDENT 3.5 .sp .EX .EE .UNINDENT .UNINDENT .INDENT 0.0 .IP \(bu 2 Framework for WebSocket servers and WebSocket subprotocols .IP \(bu 2 Implementation of \X'tty: link http://datatracker.ietf.org/doc/rfc6455/?include_text=1'\fI\%RFC6455\fP\X'tty: link' and Hybi\-10+ .IP \(bu 2 \X'tty: link http://www.gevent.org'\fI\%gevent\fP\X'tty: link' based: high performance, asynchronous .IP \(bu 2 standards conformance (100% passes the \X'tty: link http://autobahn.ws/testsuite'\fI\%Autobahn Websocket Testsuite\fP\X'tty: link') .UNINDENT .SH DISTRIBUTE & PIP .sp Installing gevent\-websocket is simple with \X'tty: link http://www.pip-installer.org'\fI\%pip\fP\X'tty: link': .INDENT 0.0 .INDENT 3.5 .sp .EX $ pip install gevent\-websocket .EE .UNINDENT .UNINDENT .SH GET THE CODE .sp Requests is being developed on BitBucket. .sp You can clone the repsistory: .INDENT 0.0 .INDENT 3.5 .sp .EX hg clone https://www.bitbucket.org/Jeffrey/gevent\-websocket .EE .UNINDENT .UNINDENT .sp or download the tarball: .INDENT 0.0 .INDENT 3.5 .sp .EX curl \-LO https://bitbucket.org/Jeffrey/gevent\-websocket/TODO .EE .UNINDENT .UNINDENT .sp Once you have a copy, you can either embed it in your application, or installed it on your system with: .INDENT 0.0 .INDENT 3.5 .sp .EX $ python setup.py install .EE .UNINDENT .UNINDENT .SH MAIN CLASSES .SH EXCEPTIONS .INDENT 0.0 .IP \(bu 2 \fI\%Index\fP .IP \(bu 2 \fI\%Module Index\fP .IP \(bu 2 \fI\%Search Page\fP .UNINDENT .SH AUTHOR Jeffrey Gelens .SH COPYRIGHT 2013, Jeffrey Gelens .\" Generated by docutils manpage writer. .