简体   繁体   English

WebSocket JavaScript:发送复杂的对象

[英]WebSocket JavaScript: Sending complex objects

I am using WebSockets as the connection between a Node.js server and my client JS code. 我将WebSockets用作Node.js服务器和客户端JS代码之间的连接。

I want to send a number of different media types (Text, Audio, Video, Images) through the socket. 我想通过套接字发送许多不同的媒体类型(文本,音频,视频,图像)。 This is not difficult of course. 当然,这并不困难。 message.data instanceof Blob separates text from media files. message.data instanceof Blob将文本与媒体文件分开。 The problem is, that I want to include several additional attributes to those media files. 问题是,我想为这些媒体文件包括几个其他属性。

Fe: 铁:

  • Dimension of an image 图片尺寸
  • Name of the image . 图像名称。 . .

Now I could send one message containing these informations in text form and follow it up with another message containing the blob. 现在,我可以以文本形式发送一条包含这些信息的消息,然后再发送一条包含blob的消息。 I would very much prefer though, to be able to build an object: 但是,我非常希望能够构建一个对象:

imageObject = {

xDimension : '50px',

yDimension : '50px', 

name : 'PinkFlowers.jpg'

imageData : fs.readFileSync(".resources/images/PinkFlowers.jpg")

}

And send this object as it is via socket.send(imageObject) . 并通过socket.send(imageObject)发送该对象。

So far so good, this actually works, but how do I collect the object and make its fields accessible in the client again? 到目前为止,它确实有效,但是如何收集对象并使其在客户端中再次可访问呢?

I have been tampering with it for a while now and I would be grateful for any ideas. 我已经对其进行了一段时间的篡改,对于任何想法我将不胜感激。

Best regards, 最好的祝福,

Sticks

Well I did get it to work using base64. 好吧,我确实使用base64使其正常工作。

On the server side I am running this piece of code: 在服务器端,我正在运行以下代码:

var imageObject = newMessageObject('img', 'flower.png');
imageObject.image = new Buffer(fs.readFileSync('./resources/images/flower.png'), 'binary').toString('base64');
imageObject.datatype = 'png';
connection.send(JSON.stringify(imageObject));

The new Buffer() is necessary to ensure a valid utf encoding. 为了确保有效的utf编码,必须使用新的Buffer()。 If used without, Chrome(dont know about Firefox and others) throws an error, that invalid utf8 encoding was detected and shuts down the execution after JSON.parse(message). 如果不使用,Chrome(不了解Firefox等)将抛出错误,表明检测到无效的utf8编码,并在JSON.parse(message)之后关闭执行。 Note: newMessageObject is just an object construction method with two fields, type and name which I use. 注意:newMessageObject只是一个对象构造方法,具有两个字段,即我使用的类型和名称。

On the client side its really straight forward: 在客户端,它的确很简单:

websocketConnection.onmessage = function(evt) {
   var message = JSON.parse(evt.data);
   ... // Some app specific stuff
   var image = new Image();
   image.onload = function() {
     canvas.getContext("2d").drawImage(image, 0, 0);
   }

   image.src = "data:image/" + message.datatype + ";base64," + message.image;

}

This draws the image on the canvas. 这会将图像绘制在画布上。

I am not convinced, that this is practicable for audio or video files, but for images it does the job. 我不相信这对于音频或视频文件是可行的,但是对于图像来说确实可以。 I will probably fall back to simply sending an obfuscated URL instead of audio/video data and read the files directly from the server. 我可能会退回到简单地发送一个混淆的URL而不是音频/视频数据,然后直接从服务器读取文件。 I dont like the security implications though. 我不喜欢安全隐患。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM