简体   繁体   English

如何将 python-socketio 与 django 和 reactjs 一起使用?

[英]How to use python-socketio with django and reactjs?

I am getting this error when I am using reactjs with django:当我将 reactjs 与 django 一起使用时出现此错误:

Not Found: /socket.io/
HTTP GET /socket.io/?EIO=4&transport=polling&t=NSwqecY 404 [0.02, 127.0.0.1:51874]

wsgi.py ==>file in django project-- wsgi.py ==> django 项目中的文件--

import os
from django.core.wsgi import get_wsgi_application
import socketio
from myapp.server import sio

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

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

server.py==> this is file in django project app server.py==> 这是 django 项目应用程序中的文件

async_mode = None

import os

from django.http import HttpResponse
import socketio

basedir = os.path.dirname(os.path.realpath(__file__))
sio = socketio.Server(async_mode=async_mode)
thread = None


global thread
if thread is None:
    thread = sio.start_background_task(background_thread)


def background_thread():
    """Example of how to send server generated events to clients."""
    count = 0
    while True:
        sio.sleep(10)
        count += 1
        sio.emit('my_response', {'data': 'Server generated event'},
                namespace='/test'


@sio.event
def close_room(sid, message):
    sio.emit('my_response',
            {'data': 'Room ' + message['room'] + ' is closing.'},
            room=message['room'])
    sio.close_room(message['room'])




@sio.event
def connect(sid, environ):
    sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)


@sio.event
def disconnect(sid):
    print('Client disconnected')


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

client.js==>start connection with calling connect function client.js==>调用connect function开始连接

connect(){
   socket = io.connect("http://localhost:8000");
   socket.on('connect', function () {
    console.log("connected to server")
 });}

What should I do?我应该怎么办? Or any other library/method recommended.或推荐的任何其他库/方法。

Thanks in advance..提前致谢..

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

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