add voice websocket instantiation

This commit is contained in:
Luna 2019-02-28 21:16:07 -03:00
parent 0d38c7cc0c
commit f3bc65302d
5 changed files with 106 additions and 10 deletions

View File

@ -44,6 +44,11 @@ class Config:
WS_HOST = 'localhost' WS_HOST = 'localhost'
WS_PORT = 5001 WS_PORT = 5001
#: Where to host the VOICE websocket?
# (a local address the server will bind to)
VWS_HOST = 'localhost'
VWS_PORT = 5003
# Postgres credentials # Postgres credentials
POSTGRES = {} POSTGRES = {}

View File

@ -52,6 +52,11 @@ class Config:
WS_HOST = '0.0.0.0' WS_HOST = '0.0.0.0'
WS_PORT = 5001 WS_PORT = 5001
#: Where to host the VOICE websocket?
# (a local address the server will bind to)
VWS_HOST = 'localhost'
VWS_PORT = 5003
#: Mediaproxy URL on the internet #: Mediaproxy URL on the internet
# mediaproxy is made to prevent client IPs being leaked. # mediaproxy is made to prevent client IPs being leaked.
MEDIA_PROXY = 'localhost:5002' MEDIA_PROXY = 'localhost:5002'

View File

@ -0,0 +1,27 @@
"""
Litecord
Copyright (C) 2018-2019 Luna Mendes
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
class VoiceWebsocket:
"""Voice websocket class."""
def __init__(self, ws, app):
self.ws = ws
self.app = app
async def run(self):
pass

View File

@ -0,0 +1,42 @@
"""
Litecord
Copyright (C) 2018-2019 Luna Mendes
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import urllib.urlparse
from litecord.voice.websocket import VoiceWebsocket
async def voice_websocket_handle(app, ws, url):
"""Main handler to instantiate a VoiceWebsocket
with the given url."""
args = urllib.parse.parse_qs(
urllib.parse.urlparse(url).query
)
try:
gw_version = args['v'][0]
except (KeyError, IndexError):
gw_version = '3'
if gw_version not in ('1', '2', '3'):
return await ws.close(1000, 'Invalid gateway version')
# TODO: select a different VoiceWebsocket runner depending on
# version. however i do not have docs on voice websockets
# earlier than v3.
vws = VoiceWebsocket(ws, app)
await vws.run()

37
run.py
View File

@ -61,7 +61,6 @@ from litecord.blueprints.user.billing_job import (
from litecord.ratelimits.handler import ratelimit_handler from litecord.ratelimits.handler import ratelimit_handler
from litecord.ratelimits.main import RatelimitManager from litecord.ratelimits.main import RatelimitManager
from litecord.gateway import websocket_handler
from litecord.errors import LitecordError from litecord.errors import LitecordError
from litecord.gateway.state_manager import StateManager from litecord.gateway.state_manager import StateManager
from litecord.storage import Storage from litecord.storage import Storage
@ -72,6 +71,9 @@ from litecord.images import IconManager
from litecord.jobs import JobManager from litecord.jobs import JobManager
from litecord.voice.manager import VoiceManager from litecord.voice.manager import VoiceManager
from litecord.gateway import websocket_handler
from litecord.voice.websocket_starter import voice_websocket_handler
from litecord.utils import LitecordJSONEncoder from litecord.utils import LitecordJSONEncoder
# setup logbook # setup logbook
@ -295,6 +297,19 @@ async def post_app_start(app_):
app_.sched.spawn(api_index(app_)) app_.sched.spawn(api_index(app_))
def start_websocket(host, port, ws_handler) -> asyncio.Future:
"""Start a websocket. Returns the websocket future"""
host, port = app.config['WS_HOST'], app.config['WS_PORT']
log.info(f'starting websocket at {host} {port}')
async def _wrapper(ws, url):
# We wrap the main websocket_handler
# so we can pass quart's app object.
await ws_handler(app, ws, url)
return websockets.serve(_wrapper, host, port)
@app.before_serving @app.before_serving
async def app_before_serving(): async def app_before_serving():
log.info('opening db') log.info('opening db')
@ -307,19 +322,21 @@ async def app_before_serving():
init_app_managers(app) init_app_managers(app)
# start the websocket, etc # start gateway websocket and voice websocket
host, port = app.config['WS_HOST'], app.config['WS_PORT'] ws_fut = start_websocket(
log.info(f'starting websocket at {host} {port}') app.config['WS_HOST'], app.config['WS_PORT'],
websocket_handler
)
async def _wrapper(ws, url): vws_fut = start_websocket(
# We wrap the main websocket_handler app.config['VWS_HOST'], app.config['VWS_PORT'],
# so we can pass quart's app object. voice_websocket_handler
await websocket_handler(app, ws, url) )
ws_future = websockets.serve(_wrapper, host, port)
await post_app_start(app) await post_app_start(app)
await ws_future await ws_fut
await vws_fut
@app.after_serving @app.after_serving