dpallot / simple-websocket-server

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

Multiple sockets problem #43

Closed fperucic closed 8 years ago

fperucic commented 8 years ago

Hi, I have a SocketWebServer to comunicate to web client, a and a have a separate socket opened in another thread for reading incoming requests from different clients.

WebServer is set to 5000 and the second socket si set to 8484. When I call server.serveforver() the second socket stops receving data. Here is the code:


HOST = '10.32.78.1'
UDP_PORT = 8484
WEB_SERVER_PORT = 5000
SOCKET_TIMEOUT = 4

class GenericThread(Thread):
    def __init__(self, start=True):
        Thread.__init__(self)
        self.running = False
        if start:
            self.start()

    def start(self):
        if not self.running:
            self.running = True
            super(GenericThread, self).start()

    def run(self):
        while self.running:
            self.loop()

    def stop(self):
        if self.running:
            self.running = False
            self.join()

    def loop(self):
        raise NotImplementedError

class SocketComunicationWorker(GenericThread):
    """ receives all the traffic """
    def __init__(self, socket):
        self.socket = socket
        super(SocketComunicationWorker, self).__init__()

    def loop(self):
        try:
            raw_response, (incoming_ip, port) = self.socket.recvfrom(1024)
            """ process the response """
            # cs.handle_socket_response(raw_response, incoming_ip, self.socket)
        except socket.timeout:
            pass

sckt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sckt.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sckt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sckt.settimeout(SOCKET_TIMEOUT)
sckt.bind((HOST, UDP_PORT))

socket_thread = SocketComunicationWorker(sckt)

srvr = RigWebserver(HOST, WEB_SERVER_PORT, WebSocket)
srvr.serveforever()

So, socket_thread is not receiving data after a to call to serveforever. Is this normal behaviour?

fperucic commented 8 years ago

closed! found a bug. My fault. I was running serveforever in the socket_thread so the reads were blocked.