MCPcopy
hub / github.com/HelloZeroNet/ZeroNet / UiWebsocket

Class UiWebsocket

src/Ui/UiWebsocket.py:26–1230  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24
25@PluginManager.acceptPlugins
26class UiWebsocket(object):
27 def __init__(self, ws, site, server, user, request):
28 self.ws = ws
29 self.site = site
30 self.user = user
31 self.log = site.log
32 self.request = request
33 self.permissions = []
34 self.server = server
35 self.next_message_id = 1
36 self.waiting_cb = {} # Waiting for callback. Key: message_id, Value: function pointer
37 self.channels = [] # Channels joined to
38 self.state = {"sending": False} # Shared state of websocket connection
39 self.send_queue = [] # Messages to send to client
40
41 # Start listener loop
42 def start(self):
43 ws = self.ws
44 if self.site.address == config.homepage and not self.site.page_requested:
45 # Add open fileserver port message or closed port error to homepage at first request after start
46 self.site.page_requested = True # Dont add connection notification anymore
47 import main
48 file_server = main.file_server
49 if not file_server.port_opened or file_server.tor_manager.start_onions is None:
50 self.site.page_requested = False # Not ready yet, check next time
51 else:
52 try:
53 self.addHomepageNotifications()
54 except Exception as err:
55 self.log.error("Uncaught Exception: " + Debug.formatException(err))
56
57 for notification in self.site.notifications: # Send pending notification messages
58 # send via WebSocket
59 self.cmd("notification", notification)
60 # just in case, log them to terminal
61 if notification[0] == "error":
62 self.log.error("\n*** %s\n" % self.dedent(notification[1]))
63
64 self.site.notifications = []
65
66 while True:
67 try:
68 if ws.closed:
69 break
70 else:
71 message = ws.receive()
72 except Exception as err:
73 self.log.error("WebSocket receive error: %s" % Debug.formatException(err))
74 break
75
76 if message:
77 try:
78 req = json.loads(message)
79 self.handleRequest(req)
80 except Exception as err:
81 if config.debug: # Allow websocket errors to appear on /Debug
82 import main
83 main.DebugHook.handleError()

Callers 2

actionWebsocketMethod · 0.90
ui_websocketFunction · 0.90

Calls

no outgoing calls

Tested by 1

ui_websocketFunction · 0.72