zoukankan      html  css  js  c++  java
  • 【从0開始Tornado建站】群聊

            群聊的前台主要代码:

    {%block content%}
    <!--<p class='text-success h3'>測试版本号,每天凌晨4:00清水,enjoy it~~:-)</p>-->
    <ul>
    	{%for i in content[19*int(num)-19:19*int(num)]%}
    	<li class='text-info'><a href='/user/{{i[1]}}' class='text-success h4'>【{{i[1]}}】</a> ({{i[3]}}) : {{i[2]}}</li>
    	{%end%}
    </ul>
    <ul class='pagination'>
    	{%for i in range(1,pages+1)%}
    	<li {%if int(num)==i%}class='active'{%end%}><a href='/chat/{{i}}'>{{i}}</a></li>
    	{%end%}
    </ul>
    
    </br>
    </br>
    <form action='/chat/1' method='post' class='well form-search'>
    	<label class='sr-only'>发表新贴</label><input type='text' name='topic' placeholder='发表新贴...'/>
    	<button type='submit' class='btn btn-danger'>发送</button>
    </form>
    {%end%}

    后台代码:

    class chatHandler(tornado.web.RequestHandler):
    	def get(self,num): #num第几页
    		name=self.get_cookie('hackerName')
    		chats=showChat()
    		n=len(chats)
    		if n%20==0:
    			pages=n//20 #pages总页数
    		else:
    			pages=n//20+1
    		self.render('chat.html',cookieName=name,content=chats,pages=pages,num=num)
    	def post(self,num):
    		name=self.get_cookie('hackerName')
    		if not name:
    			return #在javascript 中推断是否已登录
    		newTopic=self.get_argument('topic')
    		insertChat(name,newTopic)
    		self.redirect('/chat/1')

    showChat函数定义例如以下:

    def showChat():
    	c.execute('select * from chat')
    	tmp=c.fetchall()
    	return tmp[::-1] #逆序,最新贴放在最上面

    insertChat函数定义例如以下:

    def insertChat(name,content):
    	now=getTime()
    	c.execute('insert into chat(name,content,time) values("'+name+'","'+content+'","'+now+'")')
    	db.commit()

    效果图例如以下:



    群聊内容是按时间逆序排的,最新的在最上面,满20条就添加�一页,默认的群聊首页就是/chat/1,即第一页。这里实现的仅仅是基本功能,之后还要加上实时刷新ajax,新消息提示,都是前端的东西,在基本功能所有实现后加上去。


    转载请注明:转自http://blog.csdn.net/littlethunder/article/details/25560003

  • 相关阅读:
    POJ 1659 Frogs' Neighborhood
    zoj 2913 Bus Pass(BFS)
    ZOJ 1008 Gnome Tetravex(DFS)
    POJ 1562 Oil Deposits (DFS)
    zoj 2165 Red and Black (DFs)poj 1979
    hdu 3954 Level up
    sgu 249 Matrix
    hdu 4417 Super Mario
    SPOJ (BNUOJ) LCM Sum
    hdu 2665 Kth number 划分树
  • 原文地址:https://www.cnblogs.com/yxwkf/p/3996076.html
Copyright © 2011-2022 走看看