shell pip install django-socketio python INSTALLED_APPS = [ ... 'django_socketio', ... ] python from django.urls import path from django_socketio import views urlpatterns = [ ... path('socket.io/', views.socketio, name='socketio'), ... ] python from django_socketio import require_websocket @require_websocket def your_view(request): if request.is_websocket(): while True: message = request.websocket.wait() request.websocket.send('Received: ' + message) else: return HttpResponseBadRequest() script var socket = io.connect('http://localhost:8000'); socket.on('connect', function() { console.log('Connected!'); }); socket.on('message', function(data) { console.log('Received: ' + data); }); socket.emit('message', 'Hello');


上一篇:
下一篇:
切换中文