title: Run WebSocket server in Thread
body: <p>this is probably more a question about threading than about my websocket.
  I'm using "SimpleWebSocket" from github ( <a href="https://github.com/dpallot/simple-websocket-server" rel="nofollow">https://github.com/dpallot/simple-websocket-server</a> )
  The example works fine:</p>
  
  <pre><code>from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
  
  class SimpleEcho(WebSocket):
  
      def handleMessage(self):
          # echo message back to client
          self.sendMessage(self.data)
  
      def handleConnected(self):
          print self.address, 'connected'
  
      def handleClose(self):
          print self.address, 'closed'
  
  server = SimpleWebSocketServer('', 8000, SimpleEcho)
  server.serveforever()
  </code></pre>
  
  <p>The Server is running, I can connect and send Messages.</p>
  
  <p>Now i try to run it as a Thread with those classes:
  This one is supposed to create many threads including the WebSocketServer</p>
  
  <pre><code>from websockethread import WebSocketThread
  class startManyThreads:
      def __init__(self):
          self.thread1 = WebSocketThread()
          self.thread1.start()
  
  if __name__ == "__main__":
      startManyThreads = startManyThreads()
  </code></pre>
  
  <p>This class should run as my thread:</p>
  
  <pre><code>import threading
  from SimpleWebSocketServer import SimpleWebSocketServer
  from webSocketServer import WebSocketServer
  
  class WebSocketThread(threading.Thread):
      def __init__(self):
          threading.Thread.__init__(self)
          server = SimpleWebSocketServer('', 8000, WebSocketServer)
          server.serveforever()
  </code></pre>
  
  <p>And this is the "customized" echo example:</p>
  
  <pre><code>from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
  
  class SimpleEcho(WebSocket):
  
      def handleMessage(self):
          # echo message back to client
          self.sendMessage(self.data)
  
      def handleConnected(self):
          print self.address, 'connected'
  
      def handleClose(self):
          print self.address, 'closed'
  </code></pre>
  
  <p>I have also tried to derive this: <code>class SimpleEcho(WebSocket, threading.Thread):</code></p>
  
  <p>Any Ideas what i'm doing wrong?
  &amp;
  Thanks alot in advance!</p>
  
  <p>Edit:
  The result when i run "simpleEcho" is that i get a prompt can connect via the websocket.html (provided on github), send and receive Messages</p>
  
  <p>The result when i put it in a thread (anyone of the 3 ways i tried) is the same behaviour except when i try to "connect" from the websocket.html i get a "error: undefined". With nmap i checked and the Server seems so be running &amp; listening to port 8000</p>
  
  <p>Edit 2: Derived new Class from SimpleWebSocketServer</p>
  
  <pre><code>import threading
  
  from SimpleWebSocketServer import SimpleWebSocketServer
  
  class ThreadSimpleWebSocketThread(threading.Thread, SimpleWebSocketServer):
      def __init__(self, serversocket):
          threading.Thread.__init__(self)
          self.serversocket = serversocket
  
      def serveforever(self):
          SimpleWebSocketServer.serversocket = self.serversocket
          SimpleWebSocketServer.selectInterval = 0.1
          SimpleWebSocketServer.listeners = [self.serversocket]
          super(ThreadSimpleWebSocketThread, self).serveforever()
  
      def run(self):
          self.serveforever()
  </code></pre>