-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Labels
invalidThis doesn't seem rightThis doesn't seem right
Description
Steps to reproduce:
- Install aiohttp>=0.15.0
- Create server.py with this code:
import asyncio
from aiohttp import web
@asyncio.coroutine
def handle(request):
name = request.match_info.get('name', "Anonymous")
text = "Hello, " + name
return web.Response(body=text.encode('utf-8'))
def wshandler(request):
msg = b"Hello"
ws = web.WebSocketResponse()
print("before start")
yield from ws.prepare(request)
print("after start")
ws.send_bytes(msg)
return ws
@asyncio.coroutine
def init(loop):
app = web.Application(loop=loop)
app.router.add_route('GET', '/echo', wshandler)
app.router.add_route('GET', '/{name}', handle)
srv = yield from loop.create_server(app.make_handler(),
'127.0.0.1', 8088)
print("Server started at http://127.0.0.1:8088")
return srv
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()
Substitute yield from ws.prepare(request)
with ws.start(request)
for older versions.
- Run the server
- Open your browser at http://127.0.0.1:8088/Marco
- Open the browser console
- Paste this code:
ws = new WebSocket("ws://127.0.0.1:8088/echo");
ws.binaryType = "arraybuffer";
ws.onopen = function (e) {
console.log("WEBSOCKET OPENED")
}
ws.onclose = function (e) {
console.log("WEBSOCKET CLOSED")
}
ws.onmessage = function (e) {
var msg_view = new DataView(e.data);
var msg_decoder = new TextDecoder("utf-8");
var msg = msg_decoder.decode(msg_view);
console.log(msg);
}
ws.onerror = function (e) {
console.log("SOMETHING WRONG WITH WEBSOCKET")
}
RESULT:
In console is logged:
WEBSOCKET OPENED
Hello
WEBSOCKET CLOSED
EXPECTED:
In console should be logged
WEBSOCKET OPENED
Hello
jzwinck and makaspacex
Metadata
Metadata
Assignees
Labels
invalidThis doesn't seem rightThis doesn't seem right