Skip to content

Commit

Permalink
create connection
Browse files Browse the repository at this point in the history
  • Loading branch information
mohyour committed Sep 13, 2022
1 parent 5f2c717 commit bc1b8a4
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 20 deletions.
33 changes: 33 additions & 0 deletions ably/realtime/connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import asyncio
import websockets
import json


class RealtimeConnection:
def __init__(self, realtime):
self.options = realtime.options
self.__ably = realtime

async def connect(self):
self.connected_future = asyncio.Future()
asyncio.create_task(self.connect_impl())
return await self.connected_future

async def connect_impl(self):
async with websockets.connect(f'wss://realtime.ably.io?key={self.ably.key}') as websocket:
self.websocket = websocket
task = asyncio.create_task(self.ws_read_loop())
await task

async def ws_read_loop(self):
while True:
raw = await self.websocket.recv()
msg = json.loads(raw)
action = msg['action']
if (action == 4): # CONNECTED
self.connected_future.set_result(msg)
return msg

@property
def ably(self):
return self.__ably
11 changes: 9 additions & 2 deletions ably/realtime/realtime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from ably.realtime.connection import RealtimeConnection
from ably.rest.auth import Auth
from ably.types.options import Options

Expand All @@ -8,7 +9,7 @@
class AblyRealtime:
"""Ably Realtime Client"""

def __init__(self, key=None, **kwargs):
def __init__(self, key=None, token=None, token_details=None, **kwargs):
"""Create an AblyRealtime instance.
:Parameters:
Expand All @@ -22,8 +23,9 @@ def __init__(self, key=None, **kwargs):
options = Options(**kwargs)

self.__auth = Auth(self, options)

self.__options = options
self.key = key
self.__connection = RealtimeConnection(self)

@property
def auth(self):
Expand All @@ -32,3 +34,8 @@ def auth(self):
@property
def options(self):
return self.__options

@property
def connection(self):
"""Returns the channels container object"""

This comment has been minimized.

Copy link
@owenpearson

owenpearson Sep 13, 2022

Member

Comment seems wrong

return self.__connection
94 changes: 76 additions & 18 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ h2 = "^4.0.0"
# Optional dependencies
pycrypto = { version = "^2.6.1", optional = true }
pycryptodome = { version = "*", optional = true }
websockets = "^10.3"

[tool.poetry.extras]
oldcrypto = ["pycrypto"]
Expand Down

0 comments on commit bc1b8a4

Please sign in to comment.