我正试图加入一个房间并向房间中的所有成员发出信号,但我遇到了麻烦。当我将room = room更改为广播=真行时,它可以工作。非常感谢您的帮助。>
服务器端:
@socketio.on("room")
def room(room):
currentroom = room["room"]
join_room(currentroom)
@socketio.on("message")
def mes(message):
room = message['room']
emit('add', {'msg' : message['msg']},room = room)
客户端:
document.addEventListener('DOMContentLoaded',() => {
var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port);
socket.on('connect', () => {
window.onclick = function (e) {
if (e.target.localName == 'a') {
room = e.target.innerHTML;
localStorage.setItem('room',room);
socket.emit('room', {'room' :room});
}
}
});
document.getElementById('new-message').onsubmit = () => {
console.log(4);
console.log(document.getElementById('input-message').value);
console.log(localStorage.getItem('room'));
socket.emit('message',{'msg' : document.getElementById('input-message').value,'room' : localStorage.getItem('room')});
};
socket.on('add', data => {
console.log(6);
var p = document.createElement("p");
p.innerHTML = data.msg;
document.querySelector(".message-side").appendChild(p);
});
});
答案 0 :(得分:0)
我最终通过防止使用e.preventDefault()重新加载来修复了该问题