【webSokect】基于django Channels的简单实现
# settings.py: INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'channels',
] ASGI_APPLICATION = "webSokect.routing.application"
# routing.py #!/usr/bin/env python
# -*- coding:utf-8 -*-
from channels.routing import ProtocolTypeRouter, URLRouter
from django.conf.urls import url
from api import consumers application = ProtocolTypeRouter({
'websocket': URLRouter([
url(r'^chat/$', consumers.ChatConsumer),
])
})
# consumers.py #!/usr/bin/env python
# -*- coding:utf-8 -*-
from channels.generic.websocket import WebsocketConsumer
from channels.exceptions import StopConsumer CLIENTS = [] class ChatConsumer(WebsocketConsumer): def websocket_connect(self, message):
self.accept()
CLIENTS.append(self) def websocket_receive(self, text_data=None, bytes_data=None):
# print("message",text_data)
for item in CLIENTS:
item.send(text_data['text']) # self.send(text_data['text']) def websocket_disconnect(self, message):
print('客户端断开连接了')
CLIENTS.remove(self)
raise StopConsumer()
# chat.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Talking</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<style>
#taking {
width: 100%;
height: 700px;
background: #42fbff;
}
</style>
</head>
<body>
<div id="app" class="container">
<div class="card-header">在线聊天室</div>
<div id="taking">
<ul id="uls"> </ul>
</div>
<div style="width: 100%;height: 80px">
<textarea type="text" id="txt" placeholder="请输入内容" style="width: 100%;height: 100%"></textarea>
</div>
<button class="btn btn-primary" style="width: 100px" id="sendMsg">发送</button>
<button class="btn btn-danger" style="width: 100px" id="close">断开连接</button>
</div>
</body>
</html>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
var ws = new WebSocket("ws://127.0.0.1:8001/chat/") // 检测是否连接成功
ws.onopden = function(event){
console.log("成功连接");
}; // 接受消息
ws.onmessage = function(event){
{#console.log("消息",event.data);#}
var tag = $("<li>" + event.data + "</li>")
{#console.log(tag)#}
{#tag.text(event.data)#}
$("#uls").append(tag)
} // 发送
$("#sendMsg").click(function () {
ws.send($("#txt").val())
}) // 关闭
$("#close").click(function () {
ws.close()
console.log("断开成功");
}) </script>
【webSokect】基于django Channels的简单实现的更多相关文章
- 基于django的自定义简单session功能
基于django的自定义简单session功能 简单思路: 1.建立自定义session数据库 2.登入时将用户名和密码存入session库 3.将自定义的随机session_id写入cookie中 ...
- 通过Django Channels设计聊天机器人WEB框架
这两个月都在忙着设计针对银联客服业务的智能聊天机器人,上一周已经交完设计报告,这一周还和部门同事一起分享了系统设计及运行效果.因为时间的关系,系统原型我使用了Flask+jQuery的组合,感觉用以原 ...
- web 框架的本质及自定义web框架 模板渲染jinja2 mvc 和 mtv框架 Django框架的下载安装 基于Django实现的一个简单示例
Django基础一之web框架的本质 本节目录 一 web框架的本质及自定义web框架 二 模板渲染JinJa2 三 MVC和MTV框架 四 Django的下载安装 五 基于Django实现的一个简单 ...
- 基于Django进行简单的微信开发
代码地址如下:http://www.demodashi.com/demo/11756.html 一.微信公众号的准备: 1. 注册 访问地址:https://mp.weixin.qq.com/ 按照提 ...
- 实时 Django 终于来了 —— Django Channels 入门指南
Reference: http://www.oschina.net/translate/in_deep_with_django_channels_the_future_of_real_time_app ...
- Django Channels 入门指南
http://www.oschina.NET/translate/in_deep_with_django_channels_the_future_of_real_time_apps_in_django ...
- 【翻译】Django Channels 官方文档 -- Tutorial
Django Channels 官方文档 https://channels.readthedocs.io/en/latest/index.html 前言: 最近课程设计需要用到 WebSocket,而 ...
- 【Django】基于Django架构网站代码的目录结构
经典的Django项目源码目录结构 Django在一个项目的目录结构划分方面缺乏必要的规范.在Django的官方文档中并没有给出大型项目的代码建议目录结构,网上的文章也是根据项目的不同结构也有适当的 ...
- 基于django的视频点播网站开发
项目名称 基于django的视频点播网站开发 项目背景 学习完毕python和django之后,想找个项目练练手,本来想写个博客项目练手,无奈别人已经写过了,所以笔者就打算写一个视频点播网站,因为笔者 ...
随机推荐
- 多测师讲解自动化_rf框架搭建_高级讲师肖sir
robot framework:自动化测试框架(简称RF框架) Python3.7 RIDE(可视化界面). Wxpython pip(在线下载) . setuptools(在线安装) . 第三方 ...
- Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
如果出现这个问题,说明你的github缺少公钥 使用 ssh -T git@gtihub.com 去测试 1.生成密钥 ssh-keygen -t rsa -C "your name&quo ...
- C语言编程丨循环链表实现约瑟夫环!真可谓无所不能的C!
循环链表 把链表的两头连接,使其成为了一个环状链表,通常称为循环链表. 和它名字的表意一样,只需要将表中最后一个结点的指针指向头结点,链表就能成环儿,下图所示. 需要注意的是,虽然循环链表成环 ...
- 用CentOS 7自制Vagrant Box文件
写在前面 利用vagrant保持开发生产环境一致是一个很好的方法,不过vagrant官网上的box文件下载是真的很慢,因此,这里教大家如何自制box文件. 这篇文章你会接触到: vagrant使用 ...
- React.Component 和 React.PureComponent 、React.memo 的区别
一 结论 React.Component 是没有做任何渲染优化的,但凡调用this.setState 就会执行render的刷新操作. React.PureComponent 是继承自Componen ...
- org.apache.rocketmq.client.exception.MQClientException: No route info of this topic, TopicTest异常解决
使用RocketMQ发送消息抛出异常,异常如下: 原因: Broker 禁止自动创建Topic,且用户没有通过手动创建此Topic,或者broker 和 Nameserver网络不通: 解决方案: 1 ...
- Hive LLAP
body { margin: 0 auto; font: 13px / 1 Helvetica, Arial, sans-serif; color: rgba(68, 68, 68, 1); padd ...
- 1024|推荐一个开源免费的Spring Boot教程
2020-1024=996! 今天,星期六,你们是否加班了?我反正加了!早上去公司开了一早上会,中午回家写下了这篇文章. 今天,我要推荐一个开源免费的Spring Boot项目,就是我最近日更的Spr ...
- pyqy5 程序实例1
import sys from PyQt5.QtWidgets import QMainWindow,QApplication from PyQt5.QtGui import QIcon class ...
- Bitmap缩放(一)
使用矩阵进行压缩,通过缩放图片尺寸,来达到压缩图片的效果,和采样率的原理一样.先用位图的option将位图压缩一半,再用matrix缩放0.3f public class MainActivity e ...