前提・実現したいこと
bottle-websocketを使用して、画像を1秒ごとに更新するwebページを作成しています。
python3, bottle, bottle-websocketを使用しています。
発生している問題・エラーメッセージ
画像が表示されません。
websocketが切断された場合にのみ、最後の1枚だけが表示されます。
ブラウザはsafariを使用していますが、google choromeでも同じ症状でした。
該当のソースコード
python3
1from bottle import route, run, get, post, request, static_file, debug, template 2from bottle.ext.websocket import GeventWebSocketServer 3from bottle.ext.websocket import websocket 4import time 5 6@route('/aaa/<filename:path>') 7def static(filename): 8 return static_file(filename, root="/Users/xxx/aaa") 9 10@get('/') 11def index(): 12 return template('index.html') 13 14img_id=0 15@get('/websocket', apply=[websocket]) 16def echo(ws): 17 global img_id 18 while img_id < 3: 19 sendtx = "./aaa/chart"+str(img_id)+".png" 20 ws.send(sendtx) 21 img_id = img_id + 1 22 time.sleep(1) 23 24run(host="localhost", port=8000, server=GeventWebSocketServer)
html
1<!doctype html> 2<head> 3 <meta charset="utf-8" /> 4 <title>WebSocket Sample</title> 5 6 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> 7 <script> 8 9 $(document).ready(function() { 10 11 // WebSocket対応ブラウザかチェック 12 if (!window.WebSocket) { 13 if (window.MozWebSocket) { 14 window.WebSocket = window.MozWebSocket; 15 } else { 16 $('#messages').append("<li>Your browser doesn't support WebSockets.</li>"); 17 } 18 } 19 20 ws = new WebSocket('ws://localhost:8000/websocket'); 21 22 // 接続時の処理 23 ws.onopen = function(evt) { 24 $('#mes01').append('<p>WebSocketを接続しました。</p>'); 25 } 26 27 ws.onmessage = function(evt) { 28 document.getElementById("im").src = evt.data 29 30 } 31 32 // 切断時の処理 33 ws.onclose = function(evt) { 34 $('#mes01').append('<p>WebSocketを切断しました。</p>'); 35 } 36 }); 37 </script> 38</head> 39 40<body> 41 <div id="mes01"></div> 42 <img id="im", src=""> 43</body> 44 45</html>
試したこと
- echo(ws)のwhile文を抜け、websocketが切断されたタイミングで、最後の一枚の画像のみがwebページ上に表示されます。
- キャッシュに存在する画像のみ、1秒ごとに表示されることは確認できました。
- 画像ファイルのパスは問題ないことは確認しました。
補足情報(FW/ツールのバージョンなど)
python3, bottle, bottle-websocket
あなたの回答
tips
プレビュー