def run_twisted(self, args):
server = self<EOL>from twisted.internet import reactor<EOL>from twisted.web import resource<EOL>from twisted.web.static import File<EOL>from twisted.web.server import Site<EOL>from autobahn.twisted.websocket import (WebSocketServerFactory,<EOL>WebSocketServerProtocol)<EOL>from autobahn.twisted.resource import WebSocketResource<EOL>class DevWebSocketHandler(WebSocketServerProtocol):<EOL><INDENT>def onConnect(self, request):<EOL><INDENT>super(DevWebSocketHandler, self).onConnect(request)<EOL>server.on_open(self)<EOL><DEDENT>def onMessage(self, payload, isBinary):<EOL><INDENT>server.on_message(self, payload)<EOL><DEDENT>def onClose(self, wasClean, code, reason):<EOL><INDENT>super(DevWebSocketHandler,self).onClose(wasClean, code, reason)<EOL>server.on_close(self)<EOL><DEDENT>def write_message(self, message, binary=False):<EOL><INDENT>self.sendMessage(message, binary)<EOL><DEDENT><DEDENT>server.call_later = reactor.callLater<EOL>server.add_callback = reactor.callFromThread<EOL>factory = WebSocketServerFactory(u"<STR_LIT>".format(self.port))<EOL>factory.protocol = DevWebSocketHandler<EOL>class MainHandler(resource.Resource):<EOL><INDENT>def render_GET(self, req):<EOL><INDENT>return str(server.index_page)<EOL><DEDENT><DEDENT>root = resource.Resource()<EOL>root.putChild("<STR_LIT>", MainHandler())<EOL>root.putChild("<STR_LIT>", WebSocketResource(factory))<EOL>reactor.listenTCP(self.port, Site(root))<EOL>print("<STR_LIT>".format(self.port))<EOL>reactor.run()<EOL>
Twisted dev server implementation