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

最新文章

  1. Java提高篇—— 简单介绍Java 的内存泄漏
  2. HOLOLENS的空间管理
  3. 第四章_PHP基本语法(2)
  4. jquery的Post方法$.post()
  5. Mvc后台接收 参数
  6. centos7.2下编译安装apache2.4
  7. FFmpeg 开发环境搭建及第一个程序 Hello FFmpeg 编写
  8. [JS]两个常用的取随机整数的函数
  9. hdu2669-Romantic-(扩展欧几里得定理)
  10. .net下的span和memory
  11. Iterm2的一些好用法
  12. Java -- 异常的捕获及处理 -- 自定义异常类
  13. 通过Intent Flags ,从桌面返回到App最后Activity
  14. WPF 设置TextBox为空时,背景为文字提示
  15. 服务器选型:x86 vs 小型机谁更胜一筹?
  16. Rhythmk 一步一步学 JAVA (18): Enum枚举学习
  17. vs2008快捷键极其技巧
  18. 【python常见面试题】之python 中对list去重的多种方法
  19. linux设置rsync+inotify实时同步文件
  20. 第一篇 Postman的初级使用之设置环境快速切换生成环境与测试环境

热门文章

  1. 多测师讲解自动化--rf断言(下)--_高级讲师肖sir
  2. Android开发还不会这些?如何面试拿高薪!
  3. Warning: Permanently added the RSA host key for IP address &#39;52.74.223.119&#39; to the list of known hosts.
  4. linux(centos8):prometheus使用alertmanager发送报警邮件(prometheus 2.18.1/alertmanager 0.20.0)
  5. linux(centos8):lnmp环境编译安装zabbix5.0
  6. Mosquitto服务器的日志分析
  7. 3.QOpenGLWidget-通过着色器来渲染渐变三角形
  8. 1.opengl绘制三角形
  9. Lambda表达式(二)
  10. 阿里云服务器SQLSERVER 2019 远程服务器环境搭建【原创】【转载请注明出处】