本文整理汇总了Python中ws4py.server.cherrypyserver.WebSocketPlugin.broadcast方法的典型用法代码示例。如果您正苦于以下问题:Python WebSocketPlugin.broadcast方法的具体用法?Python WebSocketPlugin.broadcast怎么用?Python WebSocketPlugin.broadcast使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ws4py.server.cherrypyserver.WebSocketPlugin
的用法示例。
在下文中一共展示了WebSocketPlugin.broadcast方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SpyDashServer
# 需要导入模块: from ws4py.server.cherrypyserver import WebSocketPlugin [as 别名]
# 或者: from ws4py.server.cherrypyserver.WebSocketPlugin import broadcast [as 别名]
class SpyDashServer(object):
"""
Server class for the SpyDash
This class encapsulates all the functionality for the SpyDash server.
It loads available modules and handles communication over Websockets
"""
def __init__(self):
"""
Find and load modules in the modules package
"""
self.wsplugin = None
self.worker = set()
pluginmanager.load_configs()
def start(self):
"""
Start the server. This blocks
"""
# Setting up plugins
pluginmanager.load_models()
pluginmanager.load_plugin_roots(self)
create_tables()
self.wsplugin = WebSocketPlugin(cherrypy.engine)
self.wsplugin.subscribe()
cherrypy.tools.websocket = WebSocketTool()
# cherrypy.config.update({"log.access_file": "access.log",
# "log.error_file": "error.log"})
cherrypy.engine.subscribe("receive", self.receive)
self.start_updater()
config = {"/ws": {"tools.websocket.on": True, "tools.websocket.handler_cls": WebSocketHandler}}
cherrypy.quickstart(self, "/", config=config)
def broadcast(self, data, module):
"""
Broadcast a message to all connected clients
:param data: Data to broadcast
:param module: reference to the calling module
"""
try:
label = pluginmanager.get_containing_pluginconfig(module).label
except AttributeError:
return
msg = {"module": label, "data": data}
try:
msg = json.dumps(msg, ensure_ascii=False)
except TypeError:
return
self.wsplugin.broadcast(msg)
def start_updater(self):
def predicate(x):
try:
return x.updater
except (TypeError, AttributeError):
return False
for plugin in pluginmanager.get_instances():
try:
for name, method in inspect.getmembers(plugin, predicate):
worker = BackgroundTask(method.interval, method)
self.worker.add(worker)
worker.start()
except (TypeError, AttributeError, StopIteration):
pass
def cancel_updater(self, functions):
"""
Cancel given updater functions or function
:param functions: The updater functions to cancel, this can either be a set of functions to cancel or a singel function
"""
try:
workers = [worker for worker in self.worker if worker.function in functions]
except TypeError:
workers = [worker for worker in self.worker if worker.function == functions]
for worker in workers:
worker.cancel()
def receive(self, client, message):
try:
payload = json.loads(str(message))
plugin_name = payload["module"]
command = payload["command"]
if plugin_name == "system":
attribute = getattr(self, command)
else:
plugin = pluginmanager.get_plugin_for_label(plugin_name)
attribute = getattr(plugin, command)
if attribute.socketexposed is True:
try:
answer = attribute(**payload["data"])
except (KeyError, TypeError):
answer = attribute()
if answer is not None:
#.........这里部分代码省略.........