{"action":"created","issue":{"url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/691","labels_url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/691/labels{/name}","comments_url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/691/comments","events_url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/691/events","html_url":"https://github.com/KeepSafe/aiohttp/issues/691","id":122778686,"number":691,"title":"Websockets are automatically closed after response","user":{"login":"MarcoSulla","id":13075999,"avatar_url":"https://avatars.githubusercontent.com/u/13075999?v=3","gravatar_id":"","url":"https://api.github.com/users/MarcoSulla","html_url":"https://github.com/MarcoSulla","followers_url":"https://api.github.com/users/MarcoSulla/followers","following_url":"https://api.github.com/users/MarcoSulla/following{/other_user}","gists_url":"https://api.github.com/users/MarcoSulla/gists{/gist_id}","starred_url":"https://api.github.com/users/MarcoSulla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MarcoSulla/subscriptions","organizations_url":"https://api.github.com/users/MarcoSulla/orgs","repos_url":"https://api.github.com/users/MarcoSulla/repos","events_url":"https://api.github.com/users/MarcoSulla/events{/privacy}","received_events_url":"https://api.github.com/users/MarcoSulla/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/KeepSafe/aiohttp/labels/invalid","name":"invalid","color":"e6e6e6"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2015-12-17T17:05:23Z","updated_at":"2015-12-17T18:36:57Z","closed_at":"2015-12-17T17:19:35Z","body":"Steps to reproduce:\r\n\r\n1. Install aiohttp>=0.15.0\r\n1. Create server.py with this code:\r\n\r\n```\r\nimport asyncio\r\nfrom aiohttp import web\r\n\r\n\r\n@asyncio.coroutine\r\ndef handle(request):\r\n    name = request.match_info.get('name', \"Anonymous\")\r\n    text = \"Hello, \" + name\r\n    return web.Response(body=text.encode('utf-8'))\r\n\r\n\r\ndef wshandler(request):\r\n    \r\n    msg = b\"Hello\"\r\n    \r\n    ws = web.WebSocketResponse()\r\n    \r\n    print(\"before start\")\r\n    yield from ws.prepare(request)\r\n    print(\"after start\")\r\n    \r\n    ws.send_bytes(msg)\r\n\r\n    return ws\r\n\r\n\r\n@asyncio.coroutine\r\ndef init(loop):\r\n    app = web.Application(loop=loop)\r\n    app.router.add_route('GET', '/echo', wshandler)\r\n    app.router.add_route('GET', '/{name}', handle)\r\n\r\n    srv = yield from loop.create_server(app.make_handler(),\r\n                                        '127.0.0.1', 8088)\r\n    print(\"Server started at http://127.0.0.1:8088\")\r\n    return srv\r\n\r\nloop = asyncio.get_event_loop()\r\nloop.run_until_complete(init(loop))\r\nloop.run_forever()\r\n```\r\n\r\nSubstitute `yield from ws.prepare(request)` with `ws.start(request)` for older versions.\r\n\r\n2. Run the server\r\n3. Open your browser at http://127.0.0.1:8088/Marco\r\n4. Open the browser console\r\n5. Paste this code:\r\n\r\n```\r\nws = new WebSocket(\"ws://127.0.0.1:8088/echo\");\r\nws.binaryType = \"arraybuffer\";\r\n\r\nws.onopen = function (e) {\r\n    console.log(\"WEBSOCKET OPENED\")\r\n}\r\n\r\nws.onclose = function (e) {\r\n    console.log(\"WEBSOCKET CLOSED\")\r\n}\r\n\r\nws.onmessage = function (e) {\r\n  var msg_view = new DataView(e.data);\r\n  var msg_decoder = new TextDecoder(\"utf-8\");\r\n  var msg = msg_decoder.decode(msg_view);\r\n  console.log(msg);\r\n}\r\n\r\nws.onerror = function (e) {\r\n    console.log(\"SOMETHING WRONG WITH WEBSOCKET\")\r\n}\r\n```\r\n\r\n## RESULT:\r\n\r\nIn console is logged:\r\n\r\nWEBSOCKET OPENED\r\nHello\r\nWEBSOCKET CLOSED\r\n\r\n## EXPECTED:\r\n\r\nIn console should be logged\r\n\r\nWEBSOCKET OPENED\r\nHello"},"comment":{"url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/comments/165541859","html_url":"https://github.com/KeepSafe/aiohttp/issues/691#issuecomment-165541859","issue_url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/691","id":165541859,"user":{"login":"redixin","id":103634,"avatar_url":"https://avatars.githubusercontent.com/u/103634?v=3","gravatar_id":"","url":"https://api.github.com/users/redixin","html_url":"https://github.com/redixin","followers_url":"https://api.github.com/users/redixin/followers","following_url":"https://api.github.com/users/redixin/following{/other_user}","gists_url":"https://api.github.com/users/redixin/gists{/gist_id}","starred_url":"https://api.github.com/users/redixin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/redixin/subscriptions","organizations_url":"https://api.github.com/users/redixin/orgs","repos_url":"https://api.github.com/users/redixin/repos","events_url":"https://api.github.com/users/redixin/events{/privacy}","received_events_url":"https://api.github.com/users/redixin/received_events","type":"User","site_admin":false},"created_at":"2015-12-17T18:36:57Z","updated_at":"2015-12-17T18:36:57Z","body":"This all is to be able to work in synchronous manner:\r\n\r\n```python\r\ndef wshandler(request):\r\n    msg = \"Hello\"\r\n    ws = web.WebSocketResponse()\r\n    print(\"before start\")\r\n    ws.start(request)\r\n    ws.send_str(msg)\r\n    a.append(ws)\r\n    while True:\r\n        try:\r\n            msg = yield from ws.receive()\r\n        except aiohttp.errors.ClientDisconnectedError:\r\n            LOG.info(\"Disconnected %s\" % ws)\r\n        if msg.tp == web.MsgType.close:\r\n            break\r\n    a.remove(ws)  # <- see\r\n    return ws\r\n```\r\n\r\nAs for me, using protocol instances with all this methods like `connection_made`, `connection_lost`, `data_received`, etc is good, and I'm ok with both approaches.\r\n\r\nIt is not better or worst, it just another."}}