Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions DDPClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import ejson
import time
import socket
import re

from ws4py.exc import WebSocketException
from ws4py.client.threadedclient import WebSocketClient
Expand All @@ -11,13 +12,13 @@

class DDPSocket(WebSocketClient, EventEmitter):
"""DDPSocket"""
def __init__(self, url, debug=False):
def __init__(self, url, debug=False, headers=None):
self.debug = debug
# by default socket connections don't timeout. this causes issues
# where reconnects can get stuck
# TODO: make this configurable?
socket.setdefaulttimeout(10)
WebSocketClient.__init__(self, url)
WebSocketClient.__init__(self, url, headers=headers)
EventEmitter.__init__(self)

def opened(self):
Expand Down Expand Up @@ -56,7 +57,7 @@ def once(self):

class DDPClient(EventEmitter):
"""An event driven ddp client"""
def __init__(self, url, auto_reconnect=True, auto_reconnect_timeout=0.5, debug=False):
def __init__(self, url, auto_reconnect=True, auto_reconnect_timeout=0.5, debug=False, headers=None):
EventEmitter.__init__(self)
self.ddpsocket = None
self._ddp_version_index = 0
Expand All @@ -67,11 +68,16 @@ def __init__(self, url, auto_reconnect=True, auto_reconnect_timeout=0.5, debug=F
self.auto_reconnect = auto_reconnect
self.auto_reconnect_timeout = auto_reconnect_timeout
self.debug = debug
self.headers = [('Host', self._get_domain_from_url(self.url))].append(headers)
self._session = None
self._uniq_id = 0
self._callbacks = {}
self._init_socket()

def _get_domain_from_url(self, url):
p = re.compile('ws.://(.*?)/')
return p.match(url).group(1)

def _init_socket(self):
"""Initialize the ddp socket"""
# destroy the connection if it already exists
Expand All @@ -83,7 +89,7 @@ def _init_socket(self):
self.ddpsocket = None

# create a ddp socket and subscribe to events
self.ddpsocket = DDPSocket(self.url, self.debug)
self.ddpsocket = DDPSocket(self.url, self.debug, headers=self.headers)
self.ddpsocket.on('received_message', self.received_message)
self.ddpsocket.on('closed', self.closed)
self.ddpsocket.on('opened', self.opened)
Expand Down