dpallot / simple-websocket-server

A python based websocket server that is simple and easy to use.
951 stars 321 forks source link

Websocket threading - how to stop a thread? #108

Open Jmmx1237 opened 2 years ago

Jmmx1237 commented 2 years ago

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()
Jmmx1237 commented 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()`
Jmmx1237 commented 2 years ago

Is there a way, to do this in multiprocessing, and how can this implement?