简体   繁体   English

如何将socketio服务器的路径设置为用户的路径?

[英]How to set the path of the socketio server to the path of the user?

So my question is how do you se the path of the user to the path where the socket broadcasts to.所以我的问题是如何将用户的路径设置为套接字广播的路径。 Right now I have 2 sockets, one default to broadcast to all channel and to work on all channels and one just to broadcast to a specific channel.现在我有 2 个 sockets,一个默认广播到所有频道并在所有频道上工作,一个只广播到特定频道。 Even when I add window.location.pathname or location.pathname it doesn't broadcast to any channel at all.即使我添加window.location.pathnamelocation.pathname它根本不会广播到任何频道。 So what can I do to fix it?那么我能做些什么来修复它呢?

My JS Code-我的 JS 代码-

document.addEventListener('DOMContentLoaded', () => {

  var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port);

  var sockit = io.connect(location.protocol + '//' + document.domain + ':' + location.port + '/' + window.location.pathname);

  var e = document.querySelector("#broadcast");

  try {
    var result = e.options[e.selectedIndex].value;
  } catch(error) {
    TypeError
  }

  socket.on('connect', () => {
    document.querySelector('#form').onsubmit = () => {
      if (result == "channel") {
        var message = document.querySelector('#message').value;
        socket.emit('submit post', {'message': message});
        document.querySelector("#message").value = '';
        return false;
      } else if (result == "worldwide") {
        var message = document.querySelector('#message').value;
        socket.emit('submit worldwide post', {'message': message});
        document.querySelector("#message").value = '';
        return false;
      } else {
        var message = document.querySelector('#message').value;
        socket.emit('submit post', {'message': message});
        document.querySelector("#message").value = '';
        return false;
      };
    };
  });

  sockit.on('announce post', data => {
    var li = document.createElement('li');
    li.innerHTML = `<strong> ${data.username} </strong> : ${data.message} <small class="float-right"> ${data.time} </small>`;
    document.querySelector('#posts').append(li);
  });

  socket.on('announce post worldwide', data => {
    var li = document.createElement('li');
    li.innerHTML = `<strong> admin </strong> : ${data.message} <small class="float-right"> Broadcasted Worldwide at ${data.time} </small>`;
    document.querySelector('#posts').append(li);
  });
});

My Flask Server Code (if that is necessary)-我的 Flask 服务器代码(如果有必要)-

@socketio.on("submit post")
def post(data):

    current_channel = session.get("current_channel")

    if len(messages_created[current_channel]) > 100:
        messages_created[current_channel].pop(0)

    messages_created[current_channel].append([session.get("username"), data["message"], datetime.now().strftime("%H:%M")])

    message = data["message"]

    time = datetime.now().strftime("%H:%M")

    emit("announce post", {"message": message, "time": time, "username": session.get("username")}, current_channel=current_channel, broadcast=True)


@socketio.on("submit worldwide post")
def worldwide(data):

    messages_created2.update({data["message"] : datetime.now().strftime("%H:%M")})

    message = data["message"]

    time = datetime.now().strftime("%H:%M")

    emit("announce post worldwide", {"message": message, "time": time}, current_channel=current_channel, broadcast=True)

Thanks in advance提前致谢

All I had to do was add the rooms feature mentioned in the socketio documentation and it would only broadcast to the one specific channel我所要做的就是添加 socketio 文档中提到的房间功能,它只会广播到一个特定的频道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM