0

I work on ubuntu 18.04. I set up a virtual environment so that I am sure to work with python3 according to the instructions found here:https://vitux.com/install-python3-on-ubuntu-and-set-up-a-virtual-programming-environment/ Installed pip and eventlet (found that in the docs, resolved earlier issues) Added eventlet code, have no clue what that is about under the hood

I can send something to the server but when this message is broadcast back to all the other clients, the sender of the message does not receive it. When I add a breakpoint the parameter is 'undefined' when I hover over it and when I hover over 'socket' I see 'disconnected: true'. I just don't know what to do about it. I added 'include_self=True' as extra parameter (found in a Miguel Grinberg post). I just don't get what I am doing wrong.

՝՝՝

Python Code

import os
import eventlet
eventlet.monkey_patch(all=False, socket=True)

from flask import Flask, render_template, session, request
from flask_socketio import SocketIO, emit, send, join_room, leave_room

app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv('secret_key')
DEBUG MODE ON

socketio = SocketIO(app)

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

@socketio.on("logdata")
def handle_message(message):
    user = message['username']
    room = message['room']
    emit("join", {"user": user, "room": room}, broadcast=True,         
        include_self=True)

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

՝՝՝ JAVASCRIPT

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

  var socket = io.connect('http://127.0.0.1:5000');

  socket.on('connect', function() {
    let submit = document.querySelector("button")
    submit.onclick = function() {
      const user = document.querySelector('input[data-
         join="user"]').value;
      const room = document.querySelector('input[data-
         join="room"]').value;
      socket.emit('logdata', {'username': user, 'room': room});
    };
  });

    socket.on("join", function(msg) {
      const li = document.createElement('li');
      li.innerHTML = `room: ${msg.room}`;
      document.querySelector('#lijst').append(li);
    });

  });

՝՝՝ HTML

<!DOCTYPE html>
<html>
  <head>
     <script cdn that includes socketio></script>
     <script src="/static/flack.js" type="text/javascript"> </script>
  </head>
  <body>
    <form>
      <input type="text" placeholder="username" data-join="user">
      <input type="text" placeholder="room" data-join="room">
      <button>submit</button>
    </form>
    <ul id="lijst">
    </ul>
  </body>
</html>

՝՝՝

1 Answer 1

0

I removed the form-tags. Now everything is sent to sender and the other clients. I suspect the default method of a form is post, but post is an http-method. Socketio is another protocol, you cannot use http and socketio at the same time. If I'm wrong, I'd love to hear more about it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .