Assumed that websockets use unicode and not ascii

This commit is contained in:
Roy Hyunjin Han 2013-04-18 08:15:47 -07:00
commit f725889de7
2 changed files with 17 additions and 11 deletions

View file

@ -1,8 +1,8 @@
= Resolve pull requests = Resolve pull requests
= Resolve pull request #6 = Resolve pull request #6
Add unit test for handling server callback Add unit test for handling server callback
+ Use json with ensure_ascii=False
+ Add emit with callback in serve_tests + Add emit with callback in serve_tests
Use json with ensure_ascii=False
Resolve issues Resolve issues
Investigate issue #8 Investigate issue #8
Examine forks Examine forks

View file

@ -1,5 +1,5 @@
import socket import socket
from anyjson 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 urllib import urlopen
@ -46,6 +46,13 @@ 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, proxies=None):
"""
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.
SocketIO('localhost', 8000, secure=True,
proxies={'https': 'https://proxy.example.com:8080'})
"""
self._socketIO = _SocketIO(host, port, secure, proxies) self._socketIO = _SocketIO(host, port, secure, proxies)
self._channelByPath = {} self._channelByPath = {}
self.define(BaseNamespace) # Define default namespace self.define(BaseNamespace) # Define default namespace
@ -67,15 +74,15 @@ class SocketIO(object):
self.disconnect() self.disconnect()
def __del__(self): def __del__(self):
self.disconnect(force=True) self.disconnect(closeSocket=False)
@property @property
def connected(self): def connected(self):
return self._socketIO.connected return self._socketIO.connected
def disconnect(self, channelPath='', force=False): def disconnect(self, channelPath='', closeSocket=True):
if self.connected: if self.connected:
self._socketIO.disconnect(channelPath, force) self._socketIO.disconnect(channelPath, closeSocket)
if channelPath: if channelPath:
del self._channelByPath[channelPath] del self._channelByPath[channelPath]
else: else:
@ -255,15 +262,14 @@ class _SocketIO(object):
self.callbackByMessageID = {} self.callbackByMessageID = {}
def __del__(self): def __del__(self):
self.disconnect(force=True) self.disconnect(closeSocket=False)
def disconnect(self, channelPath='', force=False): def disconnect(self, channelPath='', closeSocket=True):
'Set force=True to skip closing the websocket'
if not self.connected: if not self.connected:
return return
if channelPath: if channelPath:
self.send_packet(0, channelPath) self.send_packet(0, channelPath)
elif not force: elif closeSocket:
self.connection.close() self.connection.close()
def connect(self, channelPath): def connect(self, channelPath):
@ -282,7 +288,7 @@ class _SocketIO(object):
data = messageData data = messageData
else: else:
code = 4 code = 4
data = dumps(messageData) data = dumps(messageData, ensure_ascii=False)
self.send_packet(code, channelPath, data, messageCallback) self.send_packet(code, channelPath, data, messageCallback)
def emit(self, eventName, *eventArguments, **eventKeywords): def emit(self, eventName, *eventArguments, **eventKeywords):
@ -292,7 +298,7 @@ class _SocketIO(object):
else: else:
messageCallback = None messageCallback = None
channelPath = eventKeywords.get('channelPath', '') channelPath = eventKeywords.get('channelPath', '')
data = dumps(dict(name=eventName, args=eventArguments)) data = dumps(dict(name=eventName, args=eventArguments), ensure_ascii=False)
self.send_packet(5, channelPath, data, messageCallback) self.send_packet(5, channelPath, data, messageCallback)
def set_messageCallback(self, callback): def set_messageCallback(self, callback):