tornado websocket在线聊天

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42694291/article/details/84107065

最基本的聊天页面:

基本层次:

chat.py     ---

static        --- favicon.ico

templates ---index.html

 

主代码chat.py:

# coding:utf-8

import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
import os
import datetime

from tornado.web import RequestHandler
from tornado.options import define, options
from tornado.websocket import WebSocketHandler

define("port", default=8000, type=int)

class IndexHandler(RequestHandler):
    def get(self):
        self.render("index.html")

class ChatHandler(WebSocketHandler):

    users = set()  # 用户ID
    client_id = 1
    def open(self):
        self.client_id = ChatHandler.client_id  #每个客户端独有的ID
        ChatHandler.client_id = ChatHandler.client_id + 1
        self.users.add(self)  # 每个request添加一个用户
        for u in self.users:  # 登录告知所有人
            u.write_message(u"[%s]-[%s]-进入聊天室" % ("游客" + str(self.client_id), datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
            

    def on_message(self, message):
        for u in self.users:  # 向在线用户广播消息
            u.write_message(u"[%s]-[%s]-说:%s" % ("游客" + str(self.client_id), datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), message))

    def on_close(self):
        self.users.remove(self) # 用户关闭连接后从容器中移除用户
        for u in self.users:
            u.write_message(u"[%s]-[%s]-离开聊天室" % ("游客" + str(self.client_id), datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))

    def check_origin(self, origin):
        return True  

if __name__ == '__main__':
    tornado.options.parse_command_line()
    app = tornado.web.Application([
            (r"/", IndexHandler),
            (r"/chat", ChatHandler),
        ],
        static_path = os.path.join(os.path.dirname(__file__), "static"),    #ico文件路径
        template_path = os.path.join(os.path.dirname(__file__), "templates"), #视图文件路径
        cookie_secret = "DONT_SHOW_SECRET"
        xsrf_cookies = True  #预防跨站攻击
        debug = True
        )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

视图文件index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
     <!-- <script src="./static/jquery-3.3.1.min.js" type="text/javascript"></script> -->
     <link rel="shortcut icon" href="../static/favicon.ico" type="image/x-icon" />
</head>
<body>
<div id="contents" style="height:500px;overflow:auto;background-color: darkgray;"></div>
<div>
    <textarea id="message"></textarea>
    <input type="button" value="发送" οnclick="sendmsg()" id="send">
    {%module xsrf_form_html()%}
</div>
</body>
<script>
    var ws = new WebSocket("ws://"+location.host+"/chat");
    var contents = document.getElementById("contents");
    var msg = document.getElementById("message");
    
    ws.onmessage = function (evt) {
        var msg1 = "<p>"+evt.data+"</p>";
        contents.innerHTML+=msg1;
    }
    // $('#send').on('keypress', function(e){
    //     if (e.keyCode == 13){
    //         sendmsg()
    //     }

    // });
    function sendmsg() {
        ws.send(msg.value);
        msg.value = "";
    }
</script>
</html>

 

展开阅读全文

没有更多推荐了,返回首页