We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
import threading import os import logging import time from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer wss = [] PORTNUM = 8001 class Echo(WebSocket): def __init__(self, state): threading.Thread.__init__(self) def controll(self, state): # Loop while True: 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): data = (self.data) func(data) print("Echoing check received data: '%s'" % state) def handleConnected(self): print(self.address, 'connected') # start a loop for each connected client 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.t2.kill() self.t2.join() print('thread killed') 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()
The text was updated successfully, but these errors were encountered:
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()`
Sorry, something went wrong.
Is there a way, to do this in multiprocessing, and how can this implement?
No branches or pull requests
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.
The text was updated successfully, but these errors were encountered: