python代码:
from threading import Lock
from flask import Flask,render_template
from flask_socketio import SocketIO, emit
from socket import *
import threading
import json
from time import sleep,ctime
async_mode = None
app = Flask(__name__)
app.debug = False
app.threaded=True
app.config['SECRET_KEY'] = 'secret!'
thread = None
thread_lock = Lock()
count = 0
tcpdict = {}
filename = 'car.json'
def read_json():
with open(filename) as pf:
numbers = json.load(pf)
print(numbers)
return numbers
socketio = SocketIO(app,async_mode=async_mode)
def background_thread():
count = 0
while True:
socketio.sleep(2)
data=read_json()
socketio.emit('server_response', data,
namespace='/test')
@app.route('/')
def index():
# return "hello flask test"
return render_template('index.html',async_mode=socketio.async_mode)
@socketio.on('connect',namespace='/test')
def test_connect():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(target=background_thread)
def main():
socketio.run(app)
if __name__ == '__main__':
main()
前端代码:
<html>
<body>
<script>
// 建立socket连接,等待服务器“推送”数据,用回调函数更新图表
$(document).ready(function () {
namespace = '/test';
var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port + namespace);
socket.on('server_response', function (res) {
console.log(res)
console.log(res.workStatusNow)
console.log(res.gps_height)
});
});
</script>
</body>
</html>