本文整理汇总了Python中subscriber.Subscriber.websocket_attach方法的典型用法代码示例。如果您正苦于以下问题:Python Subscriber.websocket_attach方法的具体用法?Python Subscriber.websocket_attach怎么用?Python Subscriber.websocket_attach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类subscriber.Subscriber
的用法示例。
在下文中一共展示了Subscriber.websocket_attach方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MyServerProtocol
# 需要导入模块: from subscriber import Subscriber [as 别名]
# 或者: from subscriber.Subscriber import websocket_attach [as 别名]
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
uid = request.params.get('uid')
if not uid:
self.close()
uid = uid[0]
self.subscriber = Subscriber()
try:
self.subscriber.websocket_attach(self, uid)
except UidNotExists:
self.close()
self.uid = uid
log.msg("Client connecting: {0}".format(request.peer))
def onOpen(self):
log.msg("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if isBinary:
log.msg("Binary message received: {0} bytes".format(len(payload)))
else:
log.msg("Text message received: {0}".format(payload.decode('utf8')))
self.subscriber.websocket_data(self, self.uid, payload)
def onClose(self, wasClean, code, reason):
if self.uid:
try:
self.subscriber.websocket_deattach(self, self.uid)
except Exception, e:
log.err(e)
log.msg("WebSocket connection closed: {0}".format(reason))