I'm working on a program that sends text msg and images to another computer through a websocket (using autobahn) whenever I detect a certain event in a video. The problem is that I only the detect the event once. I have tried to pause the program before stopping it or sending a close handshake from the client, but it doesn't change anything. Here are the some parts of the code for the client and the server. Mind here that the if-condition is originally in a while loop. If someone has an idea, I would welcome it.
Client:
import sys
from twisted.python import log
from twisted.internet import reactor
import time
from autobahn.twisted.websocket import WebSocketClientProtocol,
WebSocketClientFactory
# import the base64 module
import base64
filename=""
class MyClientProtocol(WebSocketClientProtocol):
def onConnect(self, response):
print("Server connected: {0}".format(response.peer))
def onOpen(self):
print("WebSocket connection open.")
def hello():
# opening the image file and encoding in base64
with open(filename, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
# printing the size of the encoded image which is sent
print("Encoded size of the sent image: {0} bytes".format(len(encoded_string)))
# sending the encoded image
self.sendMessage(encoded_string)
self.sendClose()
hello()
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
# printing the size of the encoded image which is received
print("Encoded size of the received image: {0} bytes".format(len(payload)))
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
if event:
cv2.imwrite(filename,frame)
log.startLogging(sys.stdout)
factory = WebSocketClientFactory("ws://localhost:9000")
factory.protocol = MyClientProtocol
reactor.connectTCP("192.168.0.37", 9000, factory) #192.168.0.22
reactor.run()
time.sleep(5)
reactor.stop()
factory.stopFactory()
Server:
from autobahn.twisted.websocket import WebSocketServerProtocol,
WebSocketServerFactory
# import the base64 module
import base64
from datetime import datetime
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {0}".format(request.peer))
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received, saving to a file")
# decode the image and save locally
now=datetime.now()
name=now.strftime("%d-%m-%Y %H:%M:%S")+".jpg"
with open(name, "wb") as image_file:
image_file.write(base64.b64decode(payload))
# echo back message verbatim
self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
if __name__ == '__main__':
import sys
from twisted.python import log
from twisted.internet import reactor
log.startLogging(sys.stdout)
factory = WebSocketServerFactory("ws://localhost:9000", debug = False)
factory.protocol = MyServerProtocol
reactor.listenTCP(9000, factory)
reactor.run()
question from:
https://stackoverflow.com/questions/65884916/how-to-handle-clients-connexion-with-autobahn-websocket 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…