I am using the following code to implement a web server using Flask and socketio.
app = Flask(__name__, static_url_path = '', static_folder = 'static', template_folder = 'templates')
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, cors_allowed_origins = '*', ping_timeout = 10, ping_interval = 1)
CORS(app)
USE_ENCRYPTION = False
class Server():
def __init__(self):
pass
@app.route("/", methods = ['GET'])
def index():
return render_template('index.html')
@socketio.on('connect', namespace = '/home')
def messaging():
#print('User connected')
pass
@socketio.on('disconnect')
def messagingOf():
#print('User disconnected')
pass
@socketio.on('my recon', namespace = '/home')
def getmes(message, methods = ['GET', 'POST']):
self.SendMessageToClient('home event', messages.messages.home_page_message, '/home')
def SendMessageToClient(self, event, message, namespace_):
if (USE_ENCRYPTION):
socketio.emit(event, crypto.Crypto.Encrypt(message), namespace = namespace_)
else:
socketio.emit(event, message, namespace = namespace_)
def ReceiveMessageFromClient(self, message):
if (USE_ENCRYPTION):
return crypto.Crypto.Decrypt(message)
return message
def Start(self):
socketio.run(app, host = '0.0.0.0', port = 5000, debug = False)
The problem is that I get the error:
line 55, in getmes
self.SendMessageToClient('home event', messages.messages.home_page_message, '/home')
NameError: name 'self' is not defined
If I use static methods instead, it works. E.g.:
@staticmethod
def SendMessageToClient(event, message, namespace_):
if (USE_ENCRYPTION):
socketio.emit(event, crypto.Crypto.Encrypt(message), namespace = namespace_)
else:
socketio.emit(event, message, namespace = namespace_)
Server.SendMessageToClient('home event', messages.messages.home_page_message, '/home')
Why do I get the NameError? Also, how can I avoid using static methods in this case?