Skip to content

How send "WebSocket Connection Close[FIN]"??? #3363

@wencan

Description

@wencan

How send "WebSocket Connection Close[FIN]"???

like:
screenshot from 2018-10-25 22-33-27

my server code:

from aiohttp import web

async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)

async def wshandle(request):
    ws = web.WebSocketResponse()
    await ws.prepare(request)

    async for msg in ws:
        print(msg.type)
        if msg.type == web.WSMsgType.text:
            await ws.send_str("Hello, {}".format(msg.data))
        elif msg.type == web.WSMsgType.binary:
            await ws.send_bytes(msg.data)
        elif msg.type in (web.WSMsgType.close, web.WSMsgType.closing, web.WSMsgType.closed):
            break

    return ws


app = web.Application(debug=True)
app.add_routes([web.get('/echo', wshandle)])

web.run_app(app, port=8090)

my client code:

import aiohttp
import asyncio

async def main():
    async with aiohttp.ClientSession() as session:
        async with session.ws_connect('http://127.0.0.1:8090/echo') as ws:
            await ws.send_str("Zhangsan")
            # await ws.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

result:
screenshot from 2018-10-25 22-32-10

Metadata

Metadata

Assignees

No one assigned

    Labels

    Stalebugneeds-infoIssue is lacking sufficient information and will be closed if not provided

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions