增加encode/decode,支持unicode的event

This commit is contained in:
msincenselee 2017-06-17 19:59:59 +08:00
parent 3d1a519760
commit 06598f7dcc

View File

@ -38,8 +38,8 @@ class RpcObject(object):
def __init__(self):
"""Constructor"""
# 默认使用msgpack作为序列化工具
#self.useMsgpack()
self.usePickle()
self.useMsgpack()
#self.usePickle()
#----------------------------------------------------------------------
def pack(self, data):
@ -84,18 +84,22 @@ class RpcObject(object):
#----------------------------------------------------------------------
def useJson(self):
"""使用json作为序列化工具"""
print 'Use Json Serialization'
self.pack = self.__jsonPack
self.unpack = self.__jsonUnpack
#----------------------------------------------------------------------
def useMsgpack(self):
"""使用msgpack作为序列化工具"""
print 'Use MsgPack Serialization'
self.pack = self.__msgpackPack
self.unpack = self.__msgpackUnpack
#----------------------------------------------------------------------
def usePickle(self):
"""使用cPickle作为序列化工具"""
print 'Use Pickle Serialization'
self.pack = self.__picklePack
self.unpack = self.__pickleUnpack
@ -185,7 +189,7 @@ class RpcServer(RpcObject):
"""
# 序列化数据
datab = self.pack(data)
topic = topic.encode('utf-8')
# 通过广播socket发送数据
self.__socketPUB.send_multipart([topic, datab])
@ -194,7 +198,6 @@ class RpcServer(RpcObject):
"""注册函数"""
self.__functions[func.__name__] = func
########################################################################
class RpcClient(RpcObject):
"""RPC客户端"""
@ -276,7 +279,8 @@ class RpcClient(RpcObject):
# 从订阅socket收取广播数据
topic, datab = self.__socketSUB.recv_multipart()
topic = topic.decode("utf-8")
# 序列化解包
data = self.unpack(datab)
@ -295,9 +299,9 @@ class RpcClient(RpcObject):
可以使用topic=''来订阅所有的主题
"""
topic = topic.encode('utf-8')
self.__socketSUB.setsockopt(zmq.SUBSCRIBE, topic)
########################################################################
class RemoteException(Exception):