Let's say the task is to write a chat server. The message comes to the server and should be transmitted to all other users. How to maintain a list of active connections in socketserver?
1 answer
It is necessary to create a dictionary in which the key is the name / login / nickname of the user, and the value is his session. How exactly this is done depends on the technology you are using.
For example, in Tornado, I used it like this:
class PlansqTornadoChat(SockJSConnection): users = dict() @tornado.gen.engine def on_message(self, msg): data = proto.json_decode(msg) if data['type'] == 'auth': if data.get('sid', None): try: ... код ... self.users[self.user_sid] = self ... еще код ... That is, the user’s SID (Second ID) was used as the value, and the specific user’s open connection was used as the value.
Please note that I used the connection in on_message instead of on_open, but there are some reasons.
- Yes, indeed, now I understood the stupidity of my question) - Jyree
|