.\" 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" "Apr 06, 2024" "0.10" "gevent-websocket" .SH NAME gevent-websocket \- gevent-websocket Documentation .sp gevent\-websocket is a \fI\%WebSocket\fP library for the \fI\%gevent\fP networking library written written and maintained by \fI\%Jeffrey Gelens\fP It is licensed under the BSD license. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C 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}) ) .ft P .fi .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 \fI\%Flask\fP follows: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C 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() .ft P .fi .UNINDENT .UNINDENT .sp Also the browser Javascript application can be very simple: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .IP \(bu 2 Framework for WebSocket servers and WebSocket subprotocols .IP \(bu 2 Implementation of \fI\%RFC6455\fP and Hybi\-10+ .IP \(bu 2 \fI\%gevent\fP based: high performance, asynchronous .IP \(bu 2 standards conformance (100% passes the \fI\%Autobahn Websocket Testsuite\fP) .UNINDENT .SH DISTRIBUTE & PIP .sp Installing gevent\-websocket is simple with \fI\%pip\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ pip install gevent\-websocket .ft P .fi .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 .nf .ft C hg clone https://www.bitbucket.org/Jeffrey/gevent\-websocket .ft P .fi .UNINDENT .UNINDENT .sp or download the tarball: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C curl \-LO https://bitbucket.org/Jeffrey/gevent\-websocket/TODO .ft P .fi .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 .nf .ft C $ python setup.py install .ft P .fi .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 2024, Jeffrey Gelens .\" Generated by docutils manpage writer. .