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?
Did you mean to use Class-Based Namespaces
? It seems that is the way to allow methods to keep the self parameter.
I found this link and I reproduce the code below:
class MyCustomNamespace(socketio.ClientNamespace):
def on_connect(self):
pass
def on_disconnect(self):
pass
def on_my_event(self, data):
self.emit('my_response', data)
sio.register_namespace(MyCustomNamespace('/chat'))
I know nothing about socketio
, but it seems to me that you pick the name of the method from the event you need to handle. All the methods being ordinary instance methods means that they have to have the self
parameter, which means that you can refer to other attributes through self
.