Join_room in flask-socketio, TypeError: 'int' object is not subscriptable

Join_room in flask-socketio, TypeError: 'int' object is not subscriptable

我正在尝试创建一个房间并通过 emit 在客户端获取输出,但它似乎不起作用。 你能告诉我它有什么问题吗?

我认为这是错误,但我不明白它是什么意思:-

participants = ns[room[0]]._fwdm.copy() if room[0] in ns else {} TypeError: 'int' object is not subscriptable

File "E:\KabirProject\WebsocketDocs\app.py", line 36, in join_roomm emit('room joined', "Heyy I've joined the room", to = room)

这是我的服务器端代码:-

from flask import Flask, render_template
from flask_socketio import SocketIO, send, emit, join_room

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

client = 0

@app.route('/')
def welcome():
   return render_template('index.html')

@app.route('/home')
def home():
   return render_template('home.html')

@socketio.on('connect')
def connect_handerler(data):
    global client
    client = client +1
    print(f"\n\n {client}\n\n")
   

@socketio.on('disconnect')
def disconnect_handeler():
    global client
    client = client -1
    print(f"\n\n {client}\n\n")
    

@socketio.on('join')
def join_roomm(data):
    room = data['room']
    join_room(room)
    emit('room joined', "Heyy I've joined the room", to = room)   //Line 36


if __name__ == '__main__':
    socketio.run(app, debug = True)

这是我的客户端代码:-

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Index</title>
</head>
<body>
Index
<button onclick="room(1)">
Room1
</button>
    <button onclick="room(2)">
Room2
    </button>
        
 <button id="message" onclick="message()">Send a message to server</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js" integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA==" crossorigin="anonymous"></script>
<script >
    var socket = io();
     socket.on('clients count', function(client){
      // console.log(client);

     });
     socket.on('message', function(data){
        console.log(data);
     });

    function room(id){
        // console.log(id);
        socket.emit('join',{"room": id});
    };

    socket.on('room joined', function(data){
        console.log(data);
    });

</script>
</body>
</html>

这是我收到的错误:-

 self.run()
  File "c:\users\dell\appdata\local\programs\python\python39\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "E:\KabirProject\WebsocketDocs\websocketsenv\lib\site-packages\socketio\server.py", line 690, in _handle_event_internal
    r = server._trigger_event(data[0], namespace, sid, *data[1:])
  File "E:\KabirProject\WebsocketDocs\websocketsenv\lib\site-packages\socketio\server.py", line 714, in _trigger_event
    return self.handlers[namespace][event](*args)
  File "E:\KabirProject\WebsocketDocs\websocketsenv\lib\site-packages\flask_socketio\__init__.py", line 284, in _handler
    return self._handle_event(handler, message, namespace, sid,
  File "E:\KabirProject\WebsocketDocs\websocketsenv\lib\site-packages\flask_socketio\__init__.py", line 767, in _handle_event
    ret = handler(*args)
  File "E:\KabirProject\WebsocketDocs\app.py", line 36, in join_roomm
    emit('room joined', "Heyy I've joined the room", to = room)
  File "E:\KabirProject\WebsocketDocs\websocketsenv\lib\site-packages\flask_socketio\__init__.py", line 839, in emit
    return socketio.emit(event, *args, namespace=namespace, to=to,
  File "E:\KabirProject\WebsocketDocs\websocketsenv\lib\site-packages\flask_socketio\__init__.py", line 462, in emit
    self.server.emit(event, *args, namespace=namespace, to=to,
  File "E:\KabirProject\WebsocketDocs\websocketsenv\lib\site-packages\socketio\server.py", line 294, in emit
    self.manager.emit(event, data, namespace, room=room,
  File "E:\KabirProject\WebsocketDocs\websocketsenv\lib\site-packages\socketio\base_manager.py", line 161, in emit
    for sid, eio_sid in self.get_participants(namespace, room):
  File "E:\KabirProject\WebsocketDocs\websocketsenv\lib\site-packages\socketio\base_manager.py", line 45, in get_participants
    participants = ns[room[0]]._fwdm.copy() if room[0] in ns else {}
TypeError: 'int' object is not subscriptable

发生这种情况是因为我试图在 to 参数中传递一个整数,我认为它只接受字符串,所以当我开始传递字符串时它开始工作了。