Skip to content

Commit

Permalink
Try websocket on the same port again
Browse files Browse the repository at this point in the history
but [Errno 48] Address already in use

For fregante/GhostText#272
  • Loading branch information
fregante committed Mar 24, 2023
1 parent 3e2cb63 commit aa94043
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions GhostText.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
from .GhostTextTools.WindowHelper import WindowHelper
from .GhostTextTools.Utils import Utils


class WebSocketServerThread(Thread):
def __init__(self, settings):
super().__init__()
self._server = WebSocketServer('localhost', 0)
self._server = WebSocketServer('localhost', 4001)
self._server.on_message(OnConnect(settings))
self._server.on_close(OnClose(settings))

Expand All @@ -33,6 +32,9 @@ def run(self):
def get_server(self):
return self._server

def get_socket(self):
return self._server._socket

class OnRequest(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if len(sublime.windows()) == 0 or self.new_window_on_connect:
Expand All @@ -41,30 +43,42 @@ def do_GET(self):
if len(self.window_command_on_connect) > 0:
sublime.active_window().run_command(self.window_command_on_connect)

web_socket_server_thread = WebSocketServerThread(self._settings)
web_socket_server_thread.start()
while not web_socket_server_thread.get_server().get_running():
sleep(0.1)

port = web_socket_server_thread.get_server().get_port()
Utils.show_status('Connection opened')

settings = sublime.load_settings('GhostText.sublime-settings')
server_port = int(settings.get('server_port', 4001))
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps({"WebSocketPort": port, "ProtocolVersion": 1}).encode())
self.wfile.write(json.dumps({"WebSocketPort": server_port, "ProtocolVersion": 1}).encode())

class MergedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
def socket(self, *args, **kwargs):
return self._socket

def __init__(self, host, port, handler_class):
settings = sublime.load_settings('GhostText.sublime-settings')
web_socket_server_thread = WebSocketServerThread(settings)
web_socket_server_thread.start()
while not web_socket_server_thread.get_server().get_running():
sleep(0.1)

self.socket = web_socket_server_thread.get_socket()

super().__init__((host, port), handler_class)

class HttpStatusServerThread(Thread):
def __init__(self, settings):
super().__init__()
server_port = int(settings.get('server_port', 4001))
server_port = settings.get('server_port', 4001)

handler = OnRequest
handler._settings = settings
handler.new_window_on_connect = bool(settings.get('new_window_on_connect', False))
handler.window_command_on_connect = str(settings.get('window_command_on_connect', 'focus_sublime_window'))
self._server = socketserver.TCPServer(("", server_port), OnRequest)
Utils.show_status('Ready on port ' + str(server_port))

self._server = MergedTCPServer("", int(server_port), OnRequest)
Utils.show_status('Ready on port ' + server_port)

def run(self):
try:
Expand Down

0 comments on commit aa94043

Please sign in to comment.