add websocket support
This commit is contained in:
parent
7401facc21
commit
24f1137db5
20
examples/ws.html
Normal file
20
examples/ws.html
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>WebSocket demo</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
var ws = new WebSocket("ws://127.0.0.1:3000/"),
|
||||||
|
messages = document.createElement('ul');
|
||||||
|
ws.onmessage = function (event) {
|
||||||
|
var messages = document.getElementsByTagName('ul')[0],
|
||||||
|
message = document.createElement('li'),
|
||||||
|
content = document.createTextNode(event.data);
|
||||||
|
message.appendChild(content);
|
||||||
|
messages.appendChild(message);
|
||||||
|
};
|
||||||
|
document.body.appendChild(messages);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
21
examples/ws.py
Normal file
21
examples/ws.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import asyncio
|
||||||
|
import sanic
|
||||||
|
from sanic.response import html
|
||||||
|
import datetime
|
||||||
|
import random
|
||||||
|
|
||||||
|
app = sanic.Sanic()
|
||||||
|
|
||||||
|
async def time(websocket, path):
|
||||||
|
while True:
|
||||||
|
now = datetime.datetime.utcnow().isoformat() + 'Z'
|
||||||
|
await websocket.send(now)
|
||||||
|
await asyncio.sleep(random.random() * 3)
|
||||||
|
|
||||||
|
app.websocket(time, 'localhost', 3000)
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def hello(request):
|
||||||
|
return html(open('ws.html').read())
|
||||||
|
|
||||||
|
app.run(port=8000)
|
|
@ -2,3 +2,4 @@ aiofiles
|
||||||
httptools
|
httptools
|
||||||
ujson
|
ujson
|
||||||
uvloop
|
uvloop
|
||||||
|
websockets
|
||||||
|
|
|
@ -17,6 +17,7 @@ from .response import HTTPResponse
|
||||||
from .router import Router
|
from .router import Router
|
||||||
from .server import serve, serve_multiple, HttpProtocol
|
from .server import serve, serve_multiple, HttpProtocol
|
||||||
from .static import register as static_register
|
from .static import register as static_register
|
||||||
|
import websockets
|
||||||
|
|
||||||
|
|
||||||
class Sanic:
|
class Sanic:
|
||||||
|
@ -44,7 +45,7 @@ class Sanic:
|
||||||
self._blueprint_order = []
|
self._blueprint_order = []
|
||||||
self.debug = None
|
self.debug = None
|
||||||
self.sock = None
|
self.sock = None
|
||||||
self.processes = None
|
self.before_start = None
|
||||||
|
|
||||||
# Register alternative method names
|
# Register alternative method names
|
||||||
self.go_fast = self.run
|
self.go_fast = self.run
|
||||||
|
@ -53,6 +54,14 @@ class Sanic:
|
||||||
# Registration
|
# Registration
|
||||||
# -------------------------------------------------------------------- #
|
# -------------------------------------------------------------------- #
|
||||||
|
|
||||||
|
# Register a websocket server
|
||||||
|
def websocket(self, handler, host='0.0.0.0', port=3000):
|
||||||
|
server = websockets.serve(handler, host, port)
|
||||||
|
|
||||||
|
def before_start(app, loop):
|
||||||
|
get_event_loop().run_until_complete(server)
|
||||||
|
self.before_start = before_start
|
||||||
|
|
||||||
# Decorator
|
# Decorator
|
||||||
def route(self, uri, methods=frozenset({'GET'}), host=None):
|
def route(self, uri, methods=frozenset({'GET'}), host=None):
|
||||||
"""
|
"""
|
||||||
|
@ -397,6 +406,14 @@ class Sanic:
|
||||||
:param protocol: Subclass of asyncio protocol class
|
:param protocol: Subclass of asyncio protocol class
|
||||||
:return: Nothing
|
:return: Nothing
|
||||||
"""
|
"""
|
||||||
|
if self.before_start is not None:
|
||||||
|
_before_start = before_start
|
||||||
|
|
||||||
|
def _before(app, loop):
|
||||||
|
if _before_start is not None:
|
||||||
|
_before_start(app, loop)
|
||||||
|
self.before_start(app, loop)
|
||||||
|
before_start = _before
|
||||||
server_settings = self._helper(
|
server_settings = self._helper(
|
||||||
host=host, port=port, debug=debug, before_start=before_start,
|
host=host, port=port, debug=debug, before_start=before_start,
|
||||||
after_start=after_start, before_stop=before_stop,
|
after_start=after_start, before_stop=before_stop,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user