用Python写了个websocket即时聊天网页(含客户端、服务端代码)
生活随笔
收集整理的這篇文章主要介紹了
用Python写了个websocket即时聊天网页(含客户端、服务端代码)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
其他的不說,原理自己搜下,直接上代碼
客戶端代碼:
<html> <head> <title>WebSocket</title><script src="jquery路徑" type="text/javascript"></script><style>*{font-family: "微軟雅黑"; margin: 0; padding: 0;}html, body{width: 100%; height: 100%; background: #000; position: relative;}.msgInfo{position: fixed; right: 2%; top: 2%; height: 96%; width: 20%; background: #666;} .msgInfo .hd{width: 100%; text-align: center; height: 40px; line-height: 40px; color: #fff; background: #333;} .msgInfo .bd{ padding: 20px; height: 100%; overflow: auto; color: #fff; line-height: 30px;} .msgInfo .bd p{margin: 5px 0;}.sendBox{position: fixed; left: 2%; bottom: 2%; width: 74%; height: 10%; background: #666; overflow: hidden;} .sendBox textarea{width: 90%; height: 100%; display: inline-block; background: #666; color: #fff; border: none; float: left;} .sendBox button{width: 10%; height: 100%; display: inline-block; background: #333; cursor: pointer; color: #fff; border: none; float: left;}.renBox{margin-left: 2%; margin-top: 2%; width: 74%; height: 84%; position: relative; overflow: hidden; float: left;} .renBox .ren{display: inline-block; position: absolute; left: 500px; top: 500px;} .renBox .ren .renHead{ display: inline-block; width: 20px; height: 20px; border-radius: 10px; background: #fff;} .renBox .ren .sayInfo{color: #fff; position: absolute; top: -30px; width: 200px; left: -90px; text-align: center;} .renBox div{color: #fff; margin-left: 100%; width: 100%; overflow: hidden; height: 30px;line-height: 30px; font-size: 20px;}.firstStep{position: fixed; width: 100%; height: 100%; z-index: 999; background: #000; text-align: center;} .firstStep .firstName{display: inline-block; padding-top: 300px;} .firstStep .firstName .username{width: 300px; height: 40px; border: 0; padding: 0 10px;} .firstStep .firstName .btn{width: 100px; height: 40px; border: 0; background: #333; color: #fff; cursor: pointer;}</style> <script> var socket; function init(username){var host = "ws://localhost:11011/name/"+username;try{ socket = new WebSocket(host); socket.onopen = function(msg){log('您已經進入聊天室')};socket.onmessage = function(msg){log(msg.data);};socket.onclose = function(msg){log("與服務器連接斷開");};}catch(ex){log(ex);}$(".sendInfo").focus(); } function send(){ var txt,msg; txt = $(".sendInfo");msg = txt.val();if(!msg){alert("Message can not be empty");return;}txt.val('');txt.focus(); try{socket.send(msg);$('.sayInfo').html(msg) // log("我說:"+msg);} catch(ex){log(ex);} } window.οnbefοreunlοad=function(){ try{ socket.send('quit'); socket.close(); socket=null; } catch(ex){ log(ex); } }; function nameok(){var _name = $('input[name="username"]').val();if(!_name){alert('請給自己取個名字吧')}else{$('.firstStep').remove()init(_name)} }function log(msg){$('.msgInfo .bd').append('<p>'+ msg +'</p>');//動畫var _html = $('<div>',{'class':'showMsg'});_html.html(msg);$('.renBox').append(_html);_html.animate({'marginLeft':'-100%'}, 10000, function(){_html.remove()}) } function show(obj){obj.fadeIn() } function onkey(event){ if(event.keyCode==13){ send(); } } </script> <link href="/static/css/css.css" type="text/css" rel="stylesheet" /> </head> <body> <div class="renBox"><!--<div class="ren">--><!--<span class="sayInfo"></span>--><!--<span class="renHead"></span>--><!--</div>--> </div> <div class="msgInfo"><div class="hd">聊天記錄</div><div class="bd"></div> </div> <div class="sendBox"><textarea class="sendInfo"></textarea><button οnclick="send()">發送</button> </div> </body> <div class="firstStep"><div class="firstName"><input type="text" class="username" name="username" placeholder="給自己取一個響亮的名字!" /><input type="button" class="btn" οnclick="nameok()" value="進入聊天" /></div> </div> </html>
總結
以上是生活随笔為你收集整理的用Python写了个websocket即时聊天网页(含客户端、服务端代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3D迷宫(二)babylon.js
- 下一篇: 2018-11-08 jsp的基本格