1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
import time
import socket
import inspect
import selectors
from typing import TYPE_CHECKING, Callable, Optional, Union
if TYPE_CHECKING:
from ._app import WebSocketApp
from . import _logging
from ._socket import send
"""
_dispatcher.py
websocket - WebSocket client library for Python
Copyright 2025 engn33r
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
class DispatcherBase:
"""
DispatcherBase
"""
def __init__(
self, app: "WebSocketApp", ping_timeout: Optional[Union[float, int]]
) -> None:
self.app = app
self.ping_timeout = ping_timeout
def timeout(self, seconds: Optional[Union[float, int]], callback: Callable) -> None:
if seconds is not None:
time.sleep(seconds)
callback()
def reconnect(self, seconds: int, reconnector: Callable) -> None:
try:
_logging.info(
f"reconnect() - retrying in {seconds} seconds [{len(inspect.stack())} frames in stack]"
)
time.sleep(seconds)
reconnector(reconnecting=True)
except KeyboardInterrupt as e:
_logging.info(f"User exited {e}")
raise e
def send(self, sock: socket.socket, data: Union[str, bytes]) -> int:
return send(sock, data)
class Dispatcher(DispatcherBase):
"""
Dispatcher
"""
def read(
self,
sock: socket.socket,
read_callback: Callable,
check_callback: Callable,
) -> None:
if self.app.sock is None or self.app.sock.sock is None:
return
sel = selectors.DefaultSelector()
sel.register(self.app.sock.sock, selectors.EVENT_READ)
try:
while self.app.keep_running:
if sel.select(self.ping_timeout):
if not read_callback():
break
check_callback()
finally:
sel.close()
class SSLDispatcher(DispatcherBase):
"""
SSLDispatcher
"""
def read(
self,
sock: socket.socket,
read_callback: Callable,
check_callback: Callable,
) -> None:
if self.app.sock is None or self.app.sock.sock is None:
return
sock = self.app.sock.sock
sel = selectors.DefaultSelector()
sel.register(sock, selectors.EVENT_READ)
try:
while self.app.keep_running:
if self.select(sock, sel):
if not read_callback():
break
check_callback()
finally:
sel.close()
def select(self, sock, sel: selectors.DefaultSelector):
if self.app.sock is None:
return None
sock = self.app.sock.sock
if sock.pending():
return [
sock,
]
r = sel.select(self.ping_timeout)
if len(r) > 0:
return r[0][0]
return None
class WrappedDispatcher:
"""
WrappedDispatcher
"""
def __init__(
self,
app: "WebSocketApp",
ping_timeout: Optional[Union[float, int]],
dispatcher,
handleDisconnect,
) -> None:
self.app = app
self.ping_timeout = ping_timeout
self.dispatcher = dispatcher
self.handleDisconnect = handleDisconnect
dispatcher.signal(2, dispatcher.abort) # keyboard interrupt
def read(
self,
sock: socket.socket,
read_callback: Callable,
check_callback: Callable,
) -> None:
self.dispatcher.read(sock, read_callback)
if self.ping_timeout:
self.timeout(self.ping_timeout, check_callback)
def send(self, sock: socket.socket, data: Union[str, bytes]) -> int:
self.dispatcher.buffwrite(sock, data, send, self.handleDisconnect)
return len(data)
def timeout(self, seconds: float, callback: Callable, *args) -> None:
self.dispatcher.timeout(seconds, callback, *args)
def reconnect(self, seconds: int, reconnector: Callable) -> None:
self.timeout(seconds, reconnector, True)
|