#!/usr/bin/env python3
import curses
-import socket
import queue
import threading
-from plomrogue.io_tcp import PlomSocket
from plomrogue.game import GameBase
from plomrogue.parser import Parser
from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex
from plomrogue.things import ThingBase
from plomrogue.misc import quote
+from plomrogue.errors import BrokenSocketConnection
+
+from ws4py.client import WebSocketBaseClient
+class WebSocketClient(WebSocketBaseClient):
+
+ def __init__(self, recv_handler, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.recv_handler = recv_handler
+ self.connect()
+
+ def received_message(self, message):
+ if message.is_text:
+ message = str(message)
+ self.recv_handler(message)
+
+ @property
+ def plom_closed(self):
+ return self.client_terminated
+
+from plomrogue.io_tcp import PlomSocket
+class PlomSocketClient(PlomSocket):
+
+ def __init__(self, recv_handler, url):
+ import socket
+ self.recv_handler = recv_handler
+ host, port = url.split(':')
+ super().__init__(socket.create_connection((host, port)))
+
+ def close(self):
+ self.socket.close()
+
+ def run(self):
+ try:
+ for msg in self.recv():
+ self.recv_handler(msg)
+ except BrokenSocketConnection:
+ pass # we assume socket will be known as dead by now
def cmd_TURN(game, n):
game.turn = n
game.tui.query_info()
player = game.get_thing(game.player_id, False)
if player.position in game.portals:
- #host, port = game.portals[player.position].split(':')
game.tui.teleport_target_host = game.portals[player.position]
- game.tui.teleport_target_port = 5000
game.tui.switch_mode('teleport')
game.turn_complete = True
game.tui.do_refresh = True
self.shows_info = shows_info
self.is_intro = is_intro
- def __init__(self, host, port):
+ def __init__(self, host):
import os
import json
self.host = host
- self.port = port
self.mode_play = self.Mode('play')
self.mode_study = self.Mode('study', shows_info=True)
self.mode_edit = self.Mode('edit')
def send(self, msg):
try:
+ if hasattr(self.socket, 'plom_closed') and self.socket.plom_closed:
+ raise BrokenSocketConnection
self.socket.send(msg)
- except BrokenPipeError:
+ except (BrokenPipeError, BrokenSocketConnection):
self.log_msg('@ server disconnected :(')
self.do_refresh = True
else:
self.log_msg('@ enter username')
elif self.mode.name == 'teleport':
- self.log_msg("@ May teleport to %s:%s" % (self.teleport_target_host,
- self.teleport_target_port));
+ self.log_msg("@ May teleport to %s" % (self.teleport_target_host)),
self.log_msg("@ Enter 'YES!' to enthusiastically affirm.");
elif self.mode.name == 'annotate' and self.explorer in self.game.info_db:
info = self.game.info_db[self.explorer]
self.log_msg(" %s - switch to play mode" % self.keys['switch_to_play']);
def loop(self, stdscr):
+ import time
def safe_addstr(y, x, line):
if y < self.size.y - 1 or x + len(line) < self.size.x:
stdscr.addstr(y, x, cut)
def connect():
- import time
- def recv_loop():
- for msg in self.socket.recv():
- if msg == 'BYE':
- break
+ def handle_recv(msg):
+ if msg == 'BYE':
+ self.socket.close()
+ else:
self.queue.put(msg)
+ socket_client_class = PlomSocketClient
+ if self.host.startswith('ws://') or self.host.startswith('wss://'):
+ socket_client_class = WebSocketClient
while True:
try:
- s = socket.create_connection((self.host, self.port))
- self.socket = PlomSocket(s)
- self.socket_thread = threading.Thread(target=recv_loop)
+ self.socket = socket_client_class(handle_recv, self.host)
+ self.socket_thread = threading.Thread(target=self.socket.run)
self.socket_thread.start()
self.switch_mode('login')
return
def reconnect():
self.send('QUIT')
+ time.sleep(0.1) # FIXME necessitated by some some strange SSL race
+ # conditions with ws4py, find out what exactly
self.switch_mode('waiting_for_server')
connect()
elif self.mode == self.mode_teleport and key == '\n':
if self.input_ == 'YES!':
self.host = self.teleport_target_host
- self.port = self.teleport_target_port
reconnect()
else:
self.log_msg('@ teleport aborted')
self.send('TASK:FLATTEN_SURROUNDINGS')
elif key in self.movement_keys:
self.send('TASK:MOVE ' + self.movement_keys[key])
+ elif key == 'q':
+ self.log_msg('quitting')
+ self.send('QUIT')
elif self.mode == self.mode_edit:
self.send('TASK:WRITE ' + key)
self.switch_mode('play')
-TUI('127.0.0.1', 5000)
+TUI('127.0.0.1:5000')