1

Please forgive me if i posted it in bad way, but i dont have any idea where is the mistake. Im doing a small chat with five rooms using Flask-Socket and SocketIO. I set rooms like this:

    ROOMS = ["waiting room", "food", "news", "games", "coding"]

Then i put them to html:

    @app.route("/chat", methods=['GET', 'POST'])
    @login_required
    def chat():

    return render_template('chat.html', username=current_user.username, rooms=ROOMS)

And on template:

    {%for room in rooms%}
            <p class="select-room"> {{ room }}</p>
          {% endfor %}

The problem is when i try to send message only to room where i am by:

    @socketio.on('message')
    def message(data):
        time_stamp = time.strftime('%b-%d %I:%M%p', time.localtime())
        send({'msg': data['msg'], 'username': data['username'], 'time_stamp':time_stamp}, room=data['room'])

and from client:

    document.querySelector('#send-message').onclick = () =>{
    socket.send({'msg': document.querySelector('#user_message').value,
    'username': username, 'room': room});
  }

It makes me key error:

     send({'msg': data['msg'], 'username': data['username'], 'time_stamp':time_stamp}, room=data['room'])
KeyError: 'room'

it also happens when i trying to switch rooms(leave one and connect other using this):

    function leaveRoom(room) {
      socket.emit('leave', {'username': username, 'room': room});

      document.querySelectorAll('.select-room').forEach(p => {
          p.style.color = "black";
      });
  }

    function joinRoom(room) {
      socket.emit('join', {'username' : username, 'room' : room});
      // Clear message area
      document.querySelector('#display-message-section').innerHTML = '';
  };

For room select i use:

    document.querySelectorAll('.select-room').forEach(p => {
      p.onclick = () => {
          let newRoom = p.innerHTML
          // Check if user already in the room
          if (newRoom == room) {
              msg = `You are already in ${room} room.`;
              printSysMsg(msg);
          } else {
              leaveRoom(room);
              joinRoom(newRoom);
              room = newRoom;
          };
      };
  });

Where im making a mistake? The full code is here:

https://pastebin.com/QXiBQG2u
https://pastebin.com/zfEtV4ZX
https://pastebin.com/wfkqNjf9
Szegerege
  • 59
  • 1
  • 7
  • The `send()` method is typically used with strings. I recommend that you change it to a named event, like your `join` and `leave`. Use a name other than `message` for this event. – Miguel Grinberg Jul 30 '19 at 15:05
  • U think that may cause the problem? And, thanks for your tutorials! Are great! – Szegerege Jul 30 '19 at 15:10
  • I'm not sure if that's the cause of the problem, but it is quite possible, because as I said, `message` has a predetermined meaning in Socket.IO. Easy enough to try, don't you think? – Miguel Grinberg Jul 31 '19 at 08:22
  • @Miguel ok i solved it, the problem was in different name. Can u help me in [link](https://stackoverflow.com/questions/57285551/setting-up-dynamic-chat-rooms-in-flasksocketio-chat) – Szegerege Jul 31 '19 at 08:37

0 Answers0