Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue with the port #3

Merged
merged 1 commit into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added logo.ico
Binary file not shown.
Binary file removed logo.png
Binary file not shown.
83 changes: 62 additions & 21 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from flask import Flask, render_template, request
from flask_sock import Sock
import socket
import os
import vgamepad as vg
import json
from pystray import Icon as icon, Menu as menu, MenuItem as item
from PIL import Image

port = os.environ.get('PORT', '80')
import threading
import ctypes

app = Flask(__name__, static_url_path='',
static_folder='static',
Expand Down Expand Up @@ -140,31 +139,73 @@ def index():
return render_template('index.html')


# run the app
hostname = socket.gethostname()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', 0))
port = sock.getsockname()[1]
sock.close()


def runServer():
print(socket.gethostbyname(socket.gethostname()))
with open('static/ip.js', 'w') as f:
f.write("let ip=\"" + socket.gethostbyname(socket.gethostname())+"\";")
print("Open this webpage in your mobile: http://"+hostname+':'+str(port))
app.run(host='0.0.0.0', port=port)


def exit(icon, item):
class thread_with_exception(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name

def run(self):

# target function of the thread class
try:
runServer()
finally:
print('ended')

def get_id(self):

# returns id of the respective thread
if hasattr(self, '_thread_id'):
return self._thread_id
for id, thread in threading._active.items():
if thread is self:
return id

def raise_exception(self):
thread_id = self.get_id()
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id,
ctypes.py_object(SystemExit))
if res > 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
print('Exception raise failure')


t = thread_with_exception('thread_with_exception')
t.start()


def exit(icon):
global t
t.raise_exception()
t.join()
icon.visible = False
icon.stop()
shutdown_func = request.environ.get('werkzeug.server.shutdown')
if shutdown_func is None:
raise RuntimeError('Not running werkzeug')
shutdown_func()


def setup(icon):
# set visible to true to show the icon
icon.visible = True
runServer()
def startIcon():
import webbrowser
icon('test', Image.open('logo.ico'), menu=menu(
item(
'http://'+hostname+':'+str(port),
action=lambda: webbrowser.open('http://'+hostname+':'+str(port))
),
item(
'Exit',
action=exit
)
)).run()


icon('test', create_image(), menu=menu(
item(
'Exit',
action=exit
))).run_detached(setup=setup)
startIcon()
1 change: 0 additions & 1 deletion static/ip.js

This file was deleted.

3 changes: 2 additions & 1 deletion static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ let buttons = {


let connected = false;
let socket = new WebSocket("ws://" + ip + '/controller');
let url = window.location.href.replace("http", "ws");
let socket = new WebSocket(url + '/controller');
socket.onopen = function (e) {
connected = true;
console.log("[open] Connection established");
Expand Down