Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

How to use python-socketio with django and reactjs?

I am getting this error when I am using reactjs with 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--

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

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

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..

question from:https://stackoverflow.com/questions/65890486/how-to-use-python-socketio-with-django-and-reactjs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...