我正在使用RPI、VPS和socket io。我想创建一个网站,用户可以去点击一个按钮,从pi得到一张照片。 我用python编写了服务器和客户机应用程序。 服务器使用socketio+flask
在服务器.py在
from flask import Flask, request, render_template
from flask_socketio import SocketIO, rooms, join_room, leave_room
app = Flask(__name__, static_url_path='/static')
app.config['SECRET_KEY'] = 'secret!'
sio = SocketIO(app)
@app.route('/')
def index():
"""Serve the client-side application."""
with open('index.html') as f:
return f.read()
# return app.send_static_file('index.html')
@sio.on('connect')
def connect():
print('Connected:')
@sio.on('join')
def on_join(room):
join_room(room)
print(request.sid + ' joined room ' + room )
@sio.on('leave')
def on_leave(room):
leave_room(room)
print(request.sid + ' left room ' + room )
@sio.on('message')
def handle_json(message):
# print('Received json: ')
# print(message)
room = rooms(request.sid)[0]
print('Forwarding to room:', room)
sio.send(message, room=room, skip_sid=request.sid, json=True)
if __name__ == '__main__':
sio.run(app, host= "142.11.210.25", port = 80)
零售物价指数_客户端.py在
^{pr2}$在索引.html在
<!DOCTYPE html>
<html>
<head>
<title>SocketIO Demo</title>
</head>
<body>
<img id="image-preview" src="" />
<button id='cam_click'>Take photo</button>
<script
src="http://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script src="/static/js/socket.io.js"></script>
<script>
var socket = io('/');
var room = 'cam_1';
function bytes2ascii(bytes) {
var str = '';
for(var i = 0; i < bytes.length; i++) {
str += String.fromCharCode(bytes[i]);
}
return str;
}
socket.on('connect', function() {
console.log(11);
socket.emit('join', room);
});
socket.on('json', function (data) {
console.log('Received:');
console.log(data);
// $('#cam_content').html(data.image);
//var encoded_image = bytes2ascii(new Uint8Array(data.image) );
var encoded_image = data.image
$('#image-preview').attr('src', `data:image/png;base64,${encoded_image}`);
});
$(document).ready(function() {
console.log('Ready...');
$('#cam_click').click(function() {
socket.send('image');
});
});
</script>
</body>
</html>
当我运行服务器和rpi客户机时,我建立了连接,当我单击按钮在索引.html,服务器fowrards到room1,我在rpi客户机上得到它,它需要一个pic,但是当它发送pic时它崩溃了,它给了我 TypeError:emit()获得意外的关键字参数“wait”
这是我运行代码时遇到的错误(rpi客户端)。在
Connection established
Received message: image
996008
Exception in thread Thread-5:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.5/dist-packages/socketio/client.py", line 514, in _handle_eio_message
self._handle_event(pkt.namespace, pkt.id, pkt.data)
File "/usr/local/lib/python3.5/dist-packages/socketio/client.py", line 413, in _handle_event
r = self._trigger_event(data[0], namespace, *data[1:])
File "/usr/local/lib/python3.5/dist-packages/socketio/client.py", line 455, in _trigger_event
return self.handlers[namespace][event](*args)
File "rpi_client2.py", line 41, in on_message
sio.send({'image': encoded_image})
File "/usr/local/lib/python3.5/dist-packages/socketio/client.py", line 296, in send
callback=callback, wait=wait, timeout=timeout)
TypeError: emit() got an unexpected keyword argument 'wait'
我按照指示安装了pythonsocketio[client]。在
什么可能导致错误?解决方法是什么?谢谢你,祝你今天愉快!在
根据python socketio doc,https://python-socketio.readthedocs.io/en/latest/client.html#emitting-events:
为了方便起见,还提供了send()方法。此方法接受数据元素作为其唯一的参数,并使用它发出标准消息事件:
在sio.发送(“一些数据”)
因此,您可以更改:
在sio.发送({'image':编码的图像})
收件人:
在二氧化硅发射('message',{'image':编码的图像})
相关问题 更多 >
编程相关推荐