Js binary data operations method, js binary operations

Source: Internet
Author: User

Js binary data operations method, js binary operations

Recently, I have made several projects to use js to operate binary data and transmit it through socket and the background. Use a blog here to make a record

First, create a new socket:

var socket=new WebSocket("ws://192.168.0.147");

Then define the function to be executed after the socket is opened and connected:

Websocket has a binaryType attribute, which can be set to "blob" or "arraybuffer". The default format is "blob". When creating a project, you forget to set it to "arraybuffer, when receiving the data below, you need to use the Blob Object to pick it up.

Socket. onopen = function () {// send the logon frame. The 4-20 bits indicate the mobile phone number var loginArr = [0X02,0X02,0X00, 0X1E, 0X20, 0X20, 0X20, 0X20, 0X20, 0X20, 0X20, 0X20, 0X20, 0X20, 0X20, 0X20, 0X20, 0X20, 0X20, 0 X, 0 X x, 0 X, 0 X, 0X00, 0X0D, 0X0A]}

The following code is forwarded to bype:

Var loginBuffer = new ArrayBuffer (30); var loginDataview = new DataView (loginBuffer); // localstorageuserinfo indicates the user's mobile phone number cached locally. var telArr = localstorageuserinfo. telPhone; var loginTime = tempTrans (); for (var I = 0; I <loginArr. length;) {loginDataview. setInt8 (I, loginArr [I]); if (I> 3 & I <(telArr. length + 4) {loginDataview. setInt8 (I, telArr. charCodeAt (I-4);} if (I> 19 & I <loginArr. length-2) {loginDataview. setInt8 (I, loginTime [I- 20]);} I ++;} // login package socket. send (loginDataview. buffer); // The formatting time is sorted by two-digit year, month, day, minute, second, and one-digit by the end. function tempTrans (time) {if (! Time) {time = new Date ();} var u32Dataview = new DataView (new Uint16Array ([time. getFullYear ()]). buffer); var uint8 = []; uint8.push (new DataView (new Uint8Array ([0X00]). buffer ). getUint8 (0) for (var I = u32Dataview. byteLength-1; I> = 0; I --) {uint8.push (u32Dataview. getUint8 (I)} uint8.push (new DataView (new Uint8Array ([time. getMonth () + 1]). buffer ). getUint8 (0); uint8.push (new DataView (new Uint8Array ([time. getDate ()]). buffer ). getUint8 (0); uint8.push (new DataView (new Uint8Array ([time. getHours ()]). buffer ). getUint8 (0); uint8.push (new DataView (new Uint8Array ([time. getMinutes ()]). buffer ). getUint8 (0); uint8.push (new DataView (new Uint8Array ([time. getSeconds ()]). buffer ). getUint8 (0); return uint8 ;}

The process of sending is probably like this, first new ArrayBuffer object, the object needs to fill in the buffer length parameters, specific view api => https://msdn.microsoft.com/zh-cn/library/br212474 (v = vs.94). aspx,

Create a new DataView object and upload ArrayBuffer. Then use the setUint and getUint methods of DataView to read the settings by bit. For details, refer to api => https://msdn.microsoft.com/zh-cn/library/br212463 (v = vs.94). aspx

The following describes how to receive data processing:

// Receives the message onmessagesocket. onmessage = function (data) {var blob _ = new Blob ([data. data]); parseBlob (blob _);} // use fileReader to operate blob Object var reader = {readAs: function (type, blob, cb) {var r = new FileReader (); r. onloadend = function () {if (typeof (cb) = 'function') {cb. call (r, r. result) ;}} try {r ['readas' + type] (blob) ;}catch (e) {}} function parseBlob (blob) {reader. readAs ('arraybuffer', blob. slice (0, blob. size), function (arr) {var dataview _ = new DataView (arr); // the second digit in the Protocol is the var socketConType = dataview _. getUint8 (1); // convert it to a string to read data var modulelength = (dataview _. buffer. byteLength-46)/33; var modulestate ={}; reader. readAs ('text', blob. slice (I * 33 + 37, I * 33 + 37 + 32), function (result) {modulestate [dataview _. getUint8 (I * 33 + 36)] = result ;});})}

You can do whatever you want after converting it into a string.

The above is the method used to operate binary data when I am working on the project. The bitwise read headers will be blown up... after google's open-source protobuf is able to set the data format, all read operations do not need to be spliced by themselves, which is very comfortable. However, I don't know the background technology. I can only eat the scum TnT behind the background bosses.

This js method for operating binary data is all the content that I have shared with you. I hope to give you a reference and support for the help house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.