Python+Django+channels实现websocket

        目前网络上多数基于django实现websocket都是老版本,而django和channels都升级到2.0+版本,导致在搭建过程中填坑无数,好在看了诸多官网文章总算搞定,都是英文,着实好了一点时间,底部附上源码,欢迎大家一起交流。

项目版本:

Python 3.6
Django 2.0.2
Channels 2.0.2
channels_redis 2.0.2
Daphne 2.0.4
asgi_redis 1.4.3
asgiref   2.1.6
pypiwin32 2.2.3

 注意事项:

1,安装channels之前,先把剩下的几个库全部安装,用PIP即可,若安装过程出现UnicodeDecodeError:‘utf-8’ codec can not decode byte 0xd6 in position 2:invalid continuation byte错误则将D:\Python36-32\Lib\site-packages\pip\compat\_init_.py中return s.decode('utf-8')修改为return s.decode('gbk')。

2,项目基于redis,各位自行安装并运行redis,运行channels时,保证redis已启动Redis启动命令,cmd进入redis安装目录(c:\\redis),Redis-server.exe redis.windows.conf  回车,正常会显示no error。默认redis已经做成服务并开机自启动,所以只需启动一次即可。

配置过程:

settings.py文件

redis_host = os.environ.get('REDIS_HOST', 'localhost')

# Channel layer definitions
# http://channels.readthedocs.io/en/latest/topics/channel_layers.html
CHANNEL_LAYERS = {
    "default": {
        # This example app uses the Redis channel layer implementation channels_redis
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [(redis_host, 6379)],
        },
    },
}

ASGI_APPLICATION = "NetWorkMonitor.routing.application"

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'channels',
    'AppMain',
    'WebSocket',
]

添加asgi.py文件,内容如下:

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "NetWorkMonitor.settings")
django.setup()
application = get_default_application()

修改wsgi.py文件,内容如下:

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

修改routing.py文件,EchoConsumer是websocket的响应类内容如下:

from django.urls import path
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from WebSocket.consumers import EchoConsumer
application = ProtocolTypeRouter({

    "websocket": AuthMiddlewareStack(
        URLRouter([
            # URLRouter just takes standard Django path() or url() entries.
            path("websockettest/", EchoConsumer),
        ]),
    ),
})
url文件增加websockettest.html访问处理,在该页面js中发起websocket连接,内容如下:
urlpatterns = [
    path('admin/', admin.site.urls),
    path('',AppMain_views.login),
    path('loginfunc/', AppMain_views.loginfunc, name='loginfunc'),
    path('websockettest/',AppMain_views.websockettest),
    path(r'^favicon\.ico$', RedirectView.as_view(url='/static/devifile_psp.ico')),
    url(r'^crossdomain.xml$',allow_domains,{
   'domains': ['172.20.0.42']}),
]
js中发起websocket连接,很简单:
socket = new WebSocket("ws://127.0.0.1:9873/websockettest/");
socket.onmessage = function(e) {
    if (socket.readyState == WebSocket.OPEN)
    {
        alert("success... content="+e.data);
    }
    else
    {
        alert("failed");
    }
    consoe.log(e.data);
}
socket.onopen = function() {
    socket.send("hello world");
}
最后还有一个响应类,实现websocket通信,注意json数据一定要使用json.dumps,这个也是版本更新所致:

 
  
class EchoConsumer(JsonWebsocketConsumer):

    def websocket_connect(self, event):
        self.accept()

    def websocket_receive(self, event):
        self.send(json.dumps({
            "type": "websocket.send supcon",
            "text": event["text"],
        }))


    def websocket_message(self, event):
        self.send(json.dumps({
            "type": "websocket.send jackie",
            "text": event["text"],
        }))

    def websocket_disconnect(self, event):
        self.send(json.dumps({
            "type": "websocket.send",
            "text": event["text"],
        }))
到此,基本上是配置完成,服务运行即可实践最新版本的websocket。
    源代码链接

你可能感兴趣的:(Python+Django+channels实现websocket)