Giter Club home page Giter Club logo

flaskws3's Introduction

flaskws3

BIG NOTE: the code is broken for some reason.

Actually good WebSocket for Flask.

Keep in mind that this is just a patched version of this Python 2 original; no changes were made, so original docs are still valid.

For convenience, the module name didn't change too.

Usage

Info is mostly taken from the original docs.

Here's some starting code for this mini tutor:

import flaskws

app = Flask(__name__)
app.wsgi_app = flaskws.WsMiddleware(app.wsgi_app)

Splitted path!

Method #1

For this method, simply create a class for your WebSocket server.

@app.route("/ws/<int:some_id>")
@flaskws.ws_server
class SampleServer():
	def __init__(self, ws_sock, **req_args):
		self.ws_sock = ws_sock
		print(req_args["some_id"])
	def on_open(self, ws_sock): pass
	def on_message(self, ws_sock, frame):
		fin, op, payload = frame # TODO: Search around code to find what info lurks in `frame`.
	def on_close(self, ws_sock): pass

Method #2

This method uses a function. A little harder to control than Method #1, but no one says you can't pick this one!..

@app.route("/ws/<int:some_id>")
@flaskws.ws_server_view
def echo_server(ws_sock, some_id=None):
	while True:
		frame = ws_sock.recv(timeout=5.0)
		if frame:
			fin, op, msg = frame
			if msg:
				ws_sock.send(msg)
				if msg == "close": # Likely a module-implemented way of closing??
					break

Client

As in the unpatched version, there's also a client. The client wasn't changed in any way. Still no SSL support though...

Here's some sample code for you:

from flaskws import ws_connect

with ws_connect("ws://example.com/wsgateway/") as ws: # Could be used as a class too, just remember to `close()`!!
	if ws.handshake():
		ws.send("somethingsomething")
		for frame in c:
			print(frame)

flaskws3's People

Contributors

smallfz avatar rixingithub 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.