将数据从浏览器端发送到zeromq节点.js客户

2024-05-21 09:00:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个捕获.js中作为脚本包含的文件索引.html它通过网络摄像头捕捉图像。我需要在用node js编写的zeromq客户机服务器(TCP client)中发送捕获的映像的base64URL,以便以后连接并将其发送到python zeromq服务器(TCP服务器)。在

这几周我尝试了很多方法,搜索了很多,zeromq没有CDN链接,无法编译它,而且变量在中显示未定义客户端.js。在

如果有人能帮我解决这个问题,我将不胜感激。我已经粘贴了下面的当前代码

这是索引.html:

 <!doctype html>
<html>
<head>
    <title>WebRTC: Still photo capture demo</title>
    <meta charset='utf-8'>
    <link rel="stylesheet" href="main.css" type="text/css" media="all">
    <script src="capture.js"></script>
    <script src="client.js"></script>
</head>
<body>
<div class="contentarea">
    <h1>
        MDN - WebRTC: Still photo capture demo
    </h1>
    <p>
        This example demonstrates how to set up a media stream using your built-in webcam, fetch an image from that stream, and create a PNG using that image.
    </p>
  <div class="camera">
    <video id="video">Video stream not available.</video>
    <button id="startbutton">Take photo</button> 
  </div>
  <canvas id="canvas">
  </canvas>
  <div class="output">
    <img id="photo" alt="The screen capture will appear in this box."> 
  </div>
    <p>
        Visit our article <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Taking_still_photos"> Taking still photos with WebRTC</a> to learn more about the technologies used here.
    </p>
</div>
</body>
</html>

这是捕获.js启动网络摄像头并拍照并保存该图像的base64URL:

^{pr2}$

这是客户端.js这是基于zeromq TCP库的客户端实现

// Hello World client
// Connects REQ socket to tcp://localhost:5555
// Sends "Hello" to server.

var zmq = require('zeromq');
 var image64;
// // socket to talk to server
console.log("Connecting to hello world server…");
var requester = zmq.socket('req');

var x = 0;
requester.on("message", function(reply) {
  console.log("Received reply", x, ": [", reply.toString(), ']');
  x += 1;
  if (x === 10) {
    requester.close();
    process.exit(0);
  }
});

requester.connect("tcp://localhost:5555");

for (var i = 0; i < 10; i++) {
  console.log("Sending request", i, '…');
  requester.send("Hello", image64);
}

process.on('SIGINT', function() {
  requester.close();
});


function sendFromClientJSTestPurpose (data) {
  image64 = data;
  console.log('hello world should contain base 64', image64);
}

这是服务器.py是zeromq server的python实现,需要从客户端接收base64URL:

#   Expects b"Hello" from client, replies with b"World"
#

import time
import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")

while True:
    #  Wait for next request from client
    message = socket.recv()
    print("Received request: %s"  %message)

    #  Do some 'work'
    time.sleep(1)

    #  Send reply back to client
    socket.send(b"World")

服务器端必须使用python中的zeromq库。客户端实现可能会有所不同

我是python和socket编程的初学者,如果您能理解这个问题,具体点,给我一个富有成效的解决方案,我将不胜感激。提前谢谢你们。你们中的许多人在知识和专业知识方面都有上帝般的深度。


Tags: todiv服务器client客户端varhtmljs