Open Jmmx1237 opened 2 years ago
Ok, I got a solution:
import threading
from threading import Thread
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
logger = logging.getLogger(__name__)
wss = []
PORTNUM = 8001
class Echo(WebSocket):
def __init__(self, state):
threading.Thread.__init__(self)
self.thread_stop = False
def controll(self, state):
# Loop
while not self.thread_stop:
self.do_something(state)
time.sleep(5)
def do_something(self, state)
for ws in wss:
ws.sendMessage(sendMessage(calculatation_something(state) #Broadcast of messane
def handleMessage(self):
if self.data is None:
self.data = ''
data = (self.data)
func(data)
print("Echoing check received data: '%s'" % state)
def handleConnected(self):
self.thread_stop = False
print(self.address, 'connected')
if self not in wss:
wss.append(self)
t2 = threading.Thread(target=self.controll, args=('on'))
t2.start()
for thread in threading.enumerate():
print(thread.name)
print("Connected")
def handleClose(self):
self.thread_stop = True
print('thread stop')
wss.remove(self)
print(self.address, 'closed')
print("Disconnected")
def main():
print("Websocket server on port %s" % PORTNUM)
server = SimpleWebSocketServer('', PORTNUM, Echo)
try:
server.serveforever()
finally:
server.close()
if __name__ == "__main__":
main()`
Is there a way, to do this in multiprocessing, and how can this implement?
Hi, is there a way, to kill a thread if a client is disconnect?
Please have a look to the handleClose section, I can't imagine how I can stop the same thread for a client, which the client has started.