[英]python socket io client emit got an unexpected keyword argument 'wait'
I am using RPI, VPS, and socket io. 我正在使用RPI,VPS和套接字io。 I want to create a website where a user can go to click a button and get a pic from the pi.
我想创建一个网站,用户可以在其中单击按钮并从pi获取图片。 I wrote the server and the client applications in python.
我用python编写了服务器和客户端应用程序。 the server uses socketio + flask
服务器使用socketio + flask
server.py server.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)
rpi_client.py rpi_client.py
import io
import time
import picamera
import socketio
import base64
sio = socketio.Client()
# Specify the room
room = 'cam_1'
socket_url = 'http://142.11.210.25:80/'
def capture_b64_image():
# Create an in-memory stream
image_stream = io.BytesIO()
# Capture image
with picamera.PiCamera() as camera:
# Camera warm-up time
time.sleep(2)
camera.capture(image_stream, 'jpeg')
# Encode the image
image_bytes = image_stream.getvalue()
return base64.b64encode(image_bytes).decode()
@sio.on('connect')
def on_connect():
print('Connection established')
sio.emit('join', room)
@sio.on('json')
def on_message(data):
print('Received message:', data)
encoded_image = capture_b64_image()
print( len(encoded_image) )
sio.send({'image': encoded_image})
@sio.on('disconnect')
def on_disconnect():
print('Disconnected from server')
sio.connect(socket_url)
sio.wait()
index.html index.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>
when i run the server and the rpi client, I get connection established, and when I click on the button to take photo in the index.html, the server fowrards to room1 and I get that on the rpi client and it takes a pic, but then it crashes when its sending the pic and it gives me TypeError: emit() got an unexpected keyword argument 'wait' 当我运行服务器和rpi客户端时,我建立了连接,并且当我单击按钮以在index.html中拍照时,服务器出现在room1上,然后在rpi客户端上得到了照片,但是当它发送图片时它崩溃了,它给了我TypeError:glow()得到了一个意外的关键字参数'wait'
here is the error I get(rpi client) when I run the codes. 这是我运行代码时得到的错误(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'
I installed python-socketio[client] as instructed. 我按照指示安装了python-socketio [client]。
What could be causing the error and whats the workaround? 是什么原因导致该错误?解决方法是什么? thank you and have a nice day!
谢谢你,祝你有美好的一天!
According to the python-socketio doc, https://python-socketio.readthedocs.io/en/latest/client.html#emitting-events : 根据python-socketio文档, https ://python-socketio.readthedocs.io/en/latest/client.html#itter-events:
For convenience, a send() method is also provided. 为了方便起见,还提供了send()方法。 This method accepts a data element as its only argument, and emits the standard message event with it:
此方法接受数据元素作为其唯一参数,并随其发出标准消息事件:
sio.send('some data') sio.send('一些数据')
Therefore, you can change: 因此,您可以更改:
sio.send({'image': encoded_image}) sio.send({'image':encode_image})
to: 至:
sio.emit('message', {'image': encoded_image}) sio.emit('message',{'image':encode_image})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.