Move websocket hub to the new gateway.

This commit is contained in:
Rapptz 2015-08-26 22:26:06 -04:00
parent af5292872b
commit 29b71a7e88
3 changed files with 15 additions and 3 deletions

View File

@ -31,7 +31,7 @@ from collections import deque
from threading import Timer from threading import Timer
from ws4py.client.threadedclient import WebSocketClient from ws4py.client.threadedclient import WebSocketClient
from sys import platform as sys_platform from sys import platform as sys_platform
from errors import InvalidEventName, InvalidDestination from errors import InvalidEventName, InvalidDestination, GatewayNotFound
from user import User from user import User
from channel import Channel, PrivateChannel from channel import Channel, PrivateChannel
from server import Server, Member, Permissions, Role from server import Server, Member, Permissions, Role
@ -100,7 +100,15 @@ class Client(object):
'on_channel_create': _null_event, 'on_channel_create': _null_event,
} }
self.ws = WebSocketClient(endpoints.WEBSOCKET_HUB, protocols=['http-only', 'chat']) gateway = requests.get(endpoints.GATEWAY)
if gateway.status_code != 200:
raise GatewayNotFound()
gateway_js = gateway.json()
url = gateway_js.get('url')
if url is None:
raise GatewayNotFound()
self.ws = WebSocketClient(url, protocols=['http-only', 'chat'])
# this is kind of hacky, but it's to avoid deadlocks. # this is kind of hacky, but it's to avoid deadlocks.
# i.e. python does not allow me to have the current thread running if it's self # i.e. python does not allow me to have the current thread running if it's self

View File

@ -24,9 +24,9 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
""" """
WEBSOCKET_HUB = 'wss://discordapp.com/hub'
BASE = 'https://discordapp.com' BASE = 'https://discordapp.com'
API_BASE = BASE + '/api' API_BASE = BASE + '/api'
GATEWAY = API_BASE + '/gateway'
USERS = API_BASE + '/users' USERS = API_BASE + '/users'
LOGIN = API_BASE + '/auth/login' LOGIN = API_BASE + '/auth/login'
LOGOUT = API_BASE + '/auth/logout' LOGOUT = API_BASE + '/auth/logout'

View File

@ -31,3 +31,7 @@ class InvalidEventName(Exception):
class InvalidDestination(Exception): class InvalidDestination(Exception):
"""Thrown when the destination from :meth:`Client.send_message` is invalid.""" """Thrown when the destination from :meth:`Client.send_message` is invalid."""
pass pass
class GatewayNotFound(Exception):
"""Thrown when the gateway hub for the :class:`Client` websocket is not found."""
pass