当前位置:网站首页>使用tornado实现本地聊天室
使用tornado实现本地聊天室
2022-07-21 05:13:00 【alading20160112】
使用tornado实现简单地本地聊天室
1.开发环境
2.tornado源码下载以及编译安装
3.聊天室全部源码
4.使用说明
1.开发环境
(1)本例程是在ubuntu12.04上面实现的
(2)python解析器是2.7版本:
在有网络的情况下可以:sudo apt-get install python
在没有网络的情况下可以从这个链接下载:
python解析器下载地址
注意请下载2.7版本的;然后解压,进入解压好的目录依次执行:
./configure –prefix=${pwd}
make
sudo make install
大功告成
2.tornado源码下载以及编译安装
(1)tornado源码的下载:
git clone git://github.com/facebook/tornado.git
(2)编译安装tornado
进入源码下载目录tornado:cd tornado
编译:sudo python setup.py build
安装:sudo python setup.py install
注意执行sudo python setup.py install的时候需要联网
3.聊天室全部源码
建立一个目录mytalk:mkdir mytalk
进入该目录:cd mytalk
建立我们的服务器python源文件:touch main.py
建立存放html的目录:mkdir myhtml
进入myhtml:cd myhtml
建立我们的html文件:touch index.html
回到mytalk,下面是我使用tree命令查看我们生成的结构:
/mytalk# tree
.
├── main.py
└── myhtml
└── index.html
我们的源码文件总共两个一个python脚本main.py,一个html文件index.html。下面是我们的源码:
#filename:main.py
#This is the server source
import tornado.web
import tornado.ioloop
class Index(tornado.web.RequestHandler):
def get(self):
self.render('myhtml/index.html')
import tornado.websocket
import json
class SocketHandler(tornado.websocket.WebSocketHandler):
clients=set()
class SocketHandler(tornado.websocket.WebSocketHandler):
clients=set()
@staticmethod
def send_to_all(message):
for c in SocketHandler.clients:
c.write_message(json.dumps(message))
def open(self):
self.write_message(json.dumps({
'type':'sys','message':'Welcome to webSocket',
}))
SocketHandler.send_to_all({
'type':'sys',
'message':str(id(self))+'has joined',
})
SocketHandler.clients.add(self)
def on_close(self):
SocketHandler.clients.remove(self)
SocketHandler.send_to_all({
'type':'sys',
'message':str(id(self))+'has left',
})
def on_message(self,message):
SocketHandler.send_to_all({
'type':'user',
'id':id(self),
'message':message
})
if __name__=="__main__":
app=tornado.web.Application([
('/',Index),
('/soc',SocketHandler),
])
app.listen(8000)
tornado.ioloop.IOLoop.instance().start()
上面使我们的server源码我们看到他用到了我们的:
def get(self):
self.render(‘myhtml/index.html’)
这个就是我们的html文件源码如下:index.html
<html>
<head>
<script> var ws = new WebSocket('ws://localhost:8000/soc'); ws.onmessage=function(event){
var table = document.getElementById('message'); var data =eval('('+ event.data+')'); ({ 'sys':function(){
var cell =table.insertRow().incertCell(); cell.innerHTML =data['message']; }, 'user':function(){
var row=table.insertRow(); row.insertCell().innerHTML=data['message']; row.insertCell().innerHTML=data['id']; }, }[data['type']])(); }; </script>
<script> function send(){
ws.send(document.getElementById('chat').value); document.getElementById('chat').value=''; } </script>
</head>
</body>
<input id='chat'><button onclick='send()'>Send</button>
<table id='message' border='1'></table>
4.使用说明
进入我们的源码目录执行main.py:suso python main.py
此时我们的聊天服务器就启动了
打开浏览器输入:http://localhost:8000/
注:多打开几个浏览器页面,这就是我们的浏览器客户端会看到如下图片:
边栏推荐
- The first batch in China! Alibaba cloud's native data Lake products have passed the evaluation and certification of the ICT Institute
- ARMv8 Cortex-a 编程向导手册学习_2
- 海外LPWAN的王者是我,一文看懂Wi-Sun协议
- ARMv8 Cortex-a 编程向导手册学习_1
- 整明白了--cache组成与工作原理
- [RPG maker MV] skill 2: use bust during task dialogue
- 《关于 SylixOS 内核存在的问题 - CAN 内核驱动》
- Wu Enda machine learning series course summary (Video + partial localization + handouts + homework)
- 基于 MS-RTOS 的 NIMBLE 开发平台
- Revisiting Markov generative architectures for effective task oriented dialog sys
猜你喜欢
基于 MS-RTOS 的 NIMBLE 开发平台
智能电表市场容量分析,年出货破亿颗
Label Embedding Online Hashing for Cross-Modal Retrieval
pycharm 安装numpy失败的解决办法
Kernel upgrade & amd NVIDIA graphics card driver & wireless network card driver and other solutions
The first batch in China! Alibaba cloud's native data Lake products have passed the evaluation and certification of the ICT Institute
什么是电子秤称重模块?它具有哪些功能?
Raspberry pie 4B new machine system configuration and computer connection operation
Raspberry pie 4B file transfer
Opencv mask wearing detection
随机推荐
【半导体先进工艺制程技术系列】HKMG工艺技术(下)
The solution to pychart's failure in installing numpy
沟道长度调制效应与短沟道效应
Driving pit of Fudan micro DW network card
Task oriented dialogue system for automatic diagnosis
树莓派4B新机系统配置与电脑连接操作
树莓派 4B 三声音传感器AO模块连接与高精度时间输出
Nimble Bluetooth development I: overview of ble Bluetooth host specification
iCaRL: Incremental Classifier and Representation Learning
嵌入式开发中,嵌入式硬件和软件有什么区别?
"In 0202, there are still people learning NFC!?"
智能电表市场容量分析,年出货破亿颗
Cortex-A53 从裸机开发_开发笔记(64位)
ROS source switching (ROS sources other than Tsinghua)
Supervised Online Hashing via Hadamard Codebook Learning
Missing sigset at compile time h xlocal. h huge_ Val.h and other solutions
Vs code space is too narrow
PLC工业控制板开发的重要性
2021 MCU WiFi竞争新格局,国产MCU WiFi芯片盘点,附录2020/2021 MCU WiFi排行
自己编写的库文件在VS code上报错:undefined reference to ‘xxx’