Skip to content

Commit

Permalink
feat(connection): forward request+client_id to life cycle methods
Browse files Browse the repository at this point in the history
Add onConnect on onClose methods to ServerProtocol and LinkProtocol

onConnect(request, client_id) -> None
onClose(client_id) -> None

They are called when a websocket connection is established or closed.  Arg
request is the aiohttp HTTP request header. client_id is an opaque string that
identifies the connection.  onC

Our use case is use an HTTP cookie in the request header to authenticate the
request.

getattr -> hasattr.
  • Loading branch information
yasushi-saito committed Dec 16, 2021
1 parent faca704 commit 6c82264
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
26 changes: 19 additions & 7 deletions python/src/wslink/backends/aiohttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,12 @@ async def handleWsRequest(self, request):

await current_ws.prepare(request)

await self.onConnect()
await self.onConnect(request, client_id)

async for msg in current_ws:
await self.onMessage(msg, client_id)

await self.onClose()
await self.onClose(client_id)

del self.connections[client_id]

Expand All @@ -246,11 +246,23 @@ async def handleWsRequest(self, request):

return current_ws

async def onConnect(self):
pass

async def onClose(self):
pass
async def onConnect(self, request, client_id):
if not self.serverProtocol:
return
if hasattr(self.serverProtocol, "onConnect"):
self.serverProtocol.onConnect(request, client_id)
for linkProtocol in self.serverProtocol.getLinkProtocols():
if hasattr(linkProtocol, "onConnect"):
linkProtocol.onConnect(request, client_id)

async def onClose(self, client_id):
if not self.serverProtocol:
return
if hasattr(self.serverProtocol, "onClose"):
self.serverProtocol.onClose(client_id)
for linkProtocol in self.serverProtocol.getLinkProtocols():
if hasattr(linkProtocol, "onClose"):
linkProtocol.onClose(client_id)

async def handleSystemMessage(self, rpcid, methodName, args, client_id):
rpcList = rpcid.split(":")
Expand Down
33 changes: 33 additions & 0 deletions python/src/wslink/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ def getSharedObject(self, key):
return self.coreServer.getSharedObject(key)
return None

def onConnect(self, request, client_id):
"""Called when a new websocket connection is established.
request is the HTTP request header, and client_id an opaque string that
identifies the connection. The default implementation is a noop. A
subclass may redefine it.
"""

pass

def onClose(self, client_id):
"""Called when a websocket connection is closed.
"""

pass

# =============================================================================
#
Expand Down Expand Up @@ -106,6 +122,23 @@ def getLinkProtocols(self):
def updateSecret(self, newSecret):
self.secret = newSecret

def onConnect(self, request, client_id):
"""Called when a new websocket connection is established.
request is the HTTP request header, and client_id an opaque string that
identifies the connection. The default implementation is a noop. A
subclass may redefine it.
"""

pass

def onClose(self, client_id):
"""Called when a websocket connection is closed.
"""

pass

@exportRpc("application.exit")
def exit(self):
"""RPC callback to exit"""
Expand Down

0 comments on commit 6c82264

Please sign in to comment.