01.websocket
websocket:https://www.cnblogs.com/xiaonq/p/12238651.html
webssh:https://www.cnblogs.com/xiaonq/p/12243024.html
2.websocket(3w1h)
什么是websocket
webSocket是一种在单个TCP连接上进行全双工通信的协议
客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据 。
浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输
websocket与http区别
http请求建立连接只能发送一次请求,不能有服务器端主动向客户端发送请求
websocket建立的长连接,一次连接,后续一直通信,这样节省资源,可以有客户端发送请求给服务器端
远古时期解决方案就是轮询
客户端以设定的时间间隔周期性地向服务端发送请求,频繁地查询是否有新的数据改动(浪费流量和资源)
webSocket应用场景?
**聊天软件:**最著名的就是微信,QQ,这一类社交聊天的app
**弹幕:**各种直播的弹幕窗口
**在线教育:**可以视频聊天、即时聊天以及其与别人合作一起在网上讨论问题…
websocket原理
websocket首先借助http协议(通过在http头部设置属性,请求和服务器进行协议升级,升级协议为websocket的应用层协议)
建立好和服务器之间的数据流,数据流之间底层还是依靠TCP协议;
websocket会接着使用这条建立好的数据流和服务器之间保持通信;
由于复杂的网络环境,数据流可能会断开,在实际使用过程中,我们在onFailure或者onClosing回调方法中,实现重连
websocket实现心跳检测的思路
通过setInterval定时任务每个3秒钟调用一次reconnect函数
reconnect会通过socket.readyState来判断这个websocket连接是否正常
如果不正常就会触发定时连接,每4s钟重试一次,直到连接成功
如果是网络断开的情况下,在指定的时间内服务器端并没有返回心跳响应消息,因此服务器端断开了。
服务断开我们使用ws.close关闭连接,在一段时间后,可以通过 onclose事件监听到。
实现聊天功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 from django.shortcuts import renderfrom dwebsocket.decorators import accept_websocket,require_websocketfrom django.http import HttpResponsedef index (request ): return render(request, 'index.html' ) from dwebsocket.backends.default.websocket import DefaultWebSocket tmp = [] @accept_websocket def echo (request ): if not request.is_websocket(): try : message = request.GET['message' ] return HttpResponse(message) except : return render(request,'index.html' ) else : '''1.实现消息推送''' tmp.append(request.websocket) for message in request.websocket: for ws in tmp: ws.send(message) '''2.实现聊天室思路'''
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 # 前端VUE代码 <template > <div > <button @click ="send" > 发消息</button > </div > </template > <script > export default { data () { return { path :"ws://127.0.0.1:8000/echo?username=zhangsan&token=xxxx" , socket :"" } }, mounted () { this .init () }, methods : { init : function ( ) { if (typeof (WebSocket ) === "undefined" ){ alert ("您的浏览器不支持socket" ) }else { this .socket = new WebSocket (this .path ) this .socket .onopen = this .open this .socket .onerror = this .error this .socket .onmessage = this .getMessage } }, open : function ( ) { console .log ("socket连接成功" ) }, error : function ( ) { console .log ("连接错误" ) }, getMessage : function (msg ) { console .log (msg.data ) }, send : function ( ) { var params = 'hahahahhahaha' ; this .socket .send (params) }, close : function ( ) { console .log ("socket已经关闭" ) } }, destroyed () { this .socket .onclose = this .close } } </script > <style > </style >
__END__