This commit is contained in:
Roy Hyunjin Han 2013-04-26 10:19:41 -07:00
commit f9e093efbb
3 changed files with 12 additions and 8 deletions

View file

@ -1,5 +1,6 @@
0.4 0.4
--- ---
- Added support for custom headers and proxies thanks to Rui and Sajal
- Added support for server-side callbacks thanks to Zac Lee - Added support for server-side callbacks thanks to Zac Lee
- Added low-level _SocketIO to remove cyclic references - Added low-level _SocketIO to remove cyclic references
- Merged Channel functionality into BaseNamespace thanks to Alexandre Bourget - Merged Channel functionality into BaseNamespace thanks to Alexandre Bourget

View file

@ -23,7 +23,7 @@ setup(
author_email='rhh@crosscompute.com', author_email='rhh@crosscompute.com',
url='https://github.com/invisibleroads/socketIO-client', url='https://github.com/invisibleroads/socketIO-client',
install_requires=[ install_requires=[
'anyjson', 'requests',
'websocket-client', 'websocket-client',
], ],
packages=find_packages(), packages=find_packages(),

View file

@ -1,8 +1,8 @@
import requests
import socket import socket
from json import dumps, loads from json import dumps, loads
from threading import Thread, Event from threading import Thread, Event
from time import sleep from time import sleep
from urllib import urlopen
from websocket import WebSocketConnectionClosedException, create_connection from websocket import WebSocketConnectionClosedException, create_connection
@ -87,7 +87,7 @@ class BaseNamespace(object): # pragma: no cover
class SocketIO(object): class SocketIO(object):
def __init__(self, host, port, secure=False, proxies=None): def __init__(self, host, port, secure=False, headers=None, proxies=None):
""" """
Create a socket.io client that connects to a socket.io server Create a socket.io client that connects to a socket.io server
at the specified host and port. Set secure=True to use HTTPS / WSS. at the specified host and port. Set secure=True to use HTTPS / WSS.
@ -95,7 +95,7 @@ class SocketIO(object):
SocketIO('localhost', 8000, secure=True, SocketIO('localhost', 8000, secure=True,
proxies={'https': 'https://proxy.example.com:8080'}) proxies={'https': 'https://proxy.example.com:8080'})
""" """
self._socketIO = _SocketIO(host, port, secure, proxies) self._socketIO = _SocketIO(host, port, secure, headers, proxies)
self._namespaceByPath = {} self._namespaceByPath = {}
self.define(BaseNamespace) # Define default namespace self.define(BaseNamespace) # Define default namespace
@ -294,17 +294,20 @@ class _SocketIO(object):
messageID = 0 messageID = 0
def __init__(self, host, port, secure, proxies): def __init__(self, host, port, secure, headers, proxies):
baseURL = '%s:%d/socket.io/%s' % (host, port, PROTOCOL) baseURL = '%s:%d/socket.io/%s' % (host, port, PROTOCOL)
targetScheme = 'https' if secure else 'http' targetScheme = 'https' if secure else 'http'
targetURL = '%s://%s/' % (targetScheme, baseURL) targetURL = '%s://%s/' % (targetScheme, baseURL)
try: try:
response = urlopen(targetURL, proxies=proxies) response = requests.get(
targetURL,
headers=headers,
proxies=proxies)
except IOError: # pragma: no cover except IOError: # pragma: no cover
raise SocketIOError('Could not start connection') raise SocketIOError('Could not start connection')
if 200 != response.getcode(): # pragma: no cover if 200 != response.status_code: # pragma: no cover
raise SocketIOError('Could not establish connection') raise SocketIOError('Could not establish connection')
responseParts = response.readline().split(':') responseParts = response.text.split(':')
sessionID = responseParts[0] sessionID = responseParts[0]
heartbeatTimeout = int(responseParts[1]) heartbeatTimeout = int(responseParts[1])
# connectionTimeout = int(responseParts[2]) # connectionTimeout = int(responseParts[2])