写了一段向 index.html 广播消息的code,并也可以正常广播到前端,
同时利用 style.css 的档案来设置网页 index.html 的样式,单独开启时可以显示所有样式,
可是当我运行flask-socketio 并用 http://127.0.0.1:5000 开启网页时,样式、颜色却全部都消失了?只剩下文字。
想请问该如何解决呢?
謝謝!
Python code:
@socketio.on('connect', namespace=name_space)
def connected_msg():
print('client connected!')
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(target = push_once)
# @app.route('/push')
def push_once():
while True:
socketio.sleep(10)
event_name = 'abcde'
t = get_price()
broadcasted_data = {'data': t}
socketio.emit(event_name, broadcasted_data, broadcast=True, namespace=name_space)
JS code:
<script>
$(document).ready(function () {
namespace = '/abcd';
var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port + namespace, {transports: ['websocket']});
socket.on('abcde', function (res) {
var t = res.data;
if (t) {
$("#test-price").prepend( ('<p>') + t );
if ($("#test-price").children().length > 5 ){
$("#test-price").children().get($("#test-price").children().length - 1).remove();
}
}
});
});
</script>