MCPcopy Create free account
hub / github.com/pywebio/PyWebIO / __init__

Method __init__

pywebio/platform/adaptor/ws.py:110–139  ·  view source on GitHub ↗
(self, connection: WebSocketConnection, application, reconnectable: bool, ioloop=None)

Source from the content-addressed store, hash-verified

108 reconnectable: bool
109
110 def __init__(self, connection: WebSocketConnection, application, reconnectable: bool, ioloop=None):
111 logger.debug("WebSocket opened")
112 self.connection = connection
113 self.reconnectable = reconnectable
114 self.session_id = connection.get_query_argument('session')
115 self.ioloop = ioloop or asyncio.get_event_loop()
116
117 if self.session_id in ('NEW', None): # 初始请求,创建新 Session
118 self._init_session(application)
119 if reconnectable:
120 _reconnect_state.active_connections[self.session_id] = self.connection
121 _reconnect_state.unclosed_sessions[self.session_id] = self.session
122 # set session id to client, so the client can send it back to server to recover a session when it
123 # resumes form a connection lost
124 connection.write_message(dict(command='set_session_id', spec=self.session_id))
125 elif self.session_id not in _reconnect_state.unclosed_sessions: # session is expired
126 bye_msg = dict(command='close_session')
127 for m in _reconnect_state.session_will_messages.get(self.session_id, [bye_msg]):
128 try:
129 connection.write_message(m)
130 except Exception:
131 logger.exception("Error in sending message via websocket")
132 else: # resumes form a connection lost
133 self.session = _reconnect_state.unclosed_sessions[self.session_id]
134 _reconnect_state.detached_sessions.pop(self.session_id, None)
135 _reconnect_state.active_connections[self.session_id] = connection
136 # send the latest messages to client
137 self._send_msg_to_client()
138
139 logger.debug('session id: %s' % self.session_id)
140
141 def _init_session(self, application):
142 session_info = self.connection.make_session_info()

Callers

nothing calls this directly

Calls 5

_init_sessionMethod · 0.95
_send_msg_to_clientMethod · 0.95
get_query_argumentMethod · 0.45
write_messageMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected