简体   繁体   中英

eventlet with django and python socket io

whenever i try connecting client to ther server, it connects succesully and when client fires an event 'chat' with some data, it gets response back as: "received your msg" + data sent "welcome"

Whenever client fires 'chat' event server(sio) also fires 'chat' event but in case server itself want to fire an event 'chat' to the client it doesn't work I tried different things like

import eventlet
eventlet.monkey_patch()

then I tried

sio.start_background_task()

after that I also tried normal threading but that doesn't worked too.

apart from that I also tried

eventlet.spawn() 

but still no improvement. How should I proceed?

socketio_app/views.py

import threading
import time
import eventlet
eventlet.monkey_patch()
from django.shortcuts import render

# Create your views here.
import socketio

sio = socketio.Server(async_mode='eventlet')


@sio.event
def connect(sid, environ):
    print('connected to ', sid)
    return True


@sio.event
def disconnect(sid):
    print("disconnected", sid)


@sio.on('chat')
def on_message(sid, data):
    print('I received a message!', data)
    sio.emit("chat", "received your msg" + str(data))
    sio.emit("chat", "welcome")

@sio.event
def bg_emit():
    print("emiitting")
    # sio.emit('chat', dict(foo='bar'))
    sio.emit('chat', data='ment')


def bkthred():
    print("strting bckgroud tsk")
    while True:
        eventlet.sleep(5)
        bg_emit()


def emit_data(data):
    print("emitting strts")
    # sio.start_background_task(target=bkthred)
    eventlet.spawn(bkthred)

supervisord.conf

[program:gunicorn]
command=gunicorn --name ProLogger-gunicorn --workers 2 ProLogger.wsgi:application --bind 0.0.0.0:8000 --timeout 100 -k eventlet
#directory=/home/ubuntu/ProLoggerBackend/
directory=/home/nimish/PycharmProjects/ProLoggerBackend/ProLogger/
stdout_logfile=/home/ubuntu/logs/gunicorn_output.log
stderr_logfile=/home/ubuntu/logs/gunicorn_error.log
autostart=true
autorestart=true
startretries=10
environment=PYTHONUNBUFFERED=1


[program:gunicornsocketio]
command=gunicorn --name ProLoggerSocket-gunicorn --workers 1 ProLogger.wsgi:socket_application --bind 0.0.0.0:5000 -k eventlet
#directory=/home/ubuntu/ProLoggerBackend/
directory=/home/nimish/PycharmProjects/ProLoggerBackend/ProLogger/
stdout_logfile=/home/ubuntu/logs/gunicornsocket_output.log
stderr_logfile=/home/ubuntu/logs/gunicornsocket_error.log
autostart=true
autorestart=true
startretries=10
environment=PYTHONUNBUFFERED=1

port 8000 runs the main application port 5000 runs socket

wsgi.py

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ProLogger.settings")
from utility.environment_utils import set_settings_module
import socketio
from socketio_app.views import sio

application = get_wsgi_application()
socket_application = socketio.WSGIApp(sio, application)

My Simple testing Client Application

import socketio

sio = socketio.Client()


@sio.event
def connect():
    print('connection established')


@sio.on("chat")
def my_message(data):
    print('message received with ', data)


@sio.event
def disconnect():
    print('disconnected farom server')


sio.connect('http://0.0.0.0:5000')

# sio.connect('http://3.17.182.118:5000')
# sio.wait()
# socketio.WSGIApp()
# socketio.Server()

sio.emit("chat", "hello")

How should I proceed further so that server itself can fire 'chat' event? Not as an acknowledgement like it is doing now(ie whenever client fires 'chat' server also fires 'chat')

So, how can I fire the event from the Django views?

https://github.com/miguelgrinberg/python-socketio/issues/155#issuecomment-353529308 This comment helped me to check myself the process id for running wsgi and socket servers The biggest blunder that I did was trying to emit from the socket server from port 8000, but the server was actually connected to port 5000. So running emit from process port 5000 solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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