JS method directly operating binary data

Xiaobian to share the method of JS directly to operate binary data, I hope everyone will read this article, let us discuss together!

The first is to create a new Socket:

 VAR socket = new WebSocket ("WS: //");   
Next defines the function of the Socket opening and after the connection:

WebSocket has a property binarytype, which can be set to “blob” or “arraybuffer”, the default format “blob “When you do a project, I forgot to set it to” ArrayBuffer “. As a result, the BLOB object needs to be used when receiving the data below.

Socket.onopen = function () {// Send login frame, 4-20 bit is mobile phone number Var loginarr = [0x02, 0x02, 0x00, 0x1e, 0x20, 0x20, 0x20 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x0a]}
The following is to transfer it to bype:

Var loginbuffer = new arraybuffer (30); var logindataview = new dataview (Loginbuffer); // localStorageUserInfo is a local user mobile phone number Var tilar = localStorageUserInfo.telphone; varLogintime = Temptrans (); for (var i = 0; i 3 && i
 19 && i  = 0; i -) {uint8.push (u32dataview.getuint8 (i))} uint8.push (new DataView ([Time.getMont () + 1]). Buffer) .Getuint8 (0)); uint8.push (new data (new data ()]). Buffer .getuint8 (0 )); uint8.push (new data)]). Buffer .getuint8 (0)); uint8.push (New DataView (NEW Time.getMinutes ()]). Buffer. Getuint8 (0)); uint8.push (New DataView (New DataView ([Time.GetSeconds ()]). Buffer .getuint8 (0)); return uint8;}     The process of sending is probably like this. First, first new ArrayBuffer object, the object needs to fill in the buffer length parameters, specifically view the API ==> https://msdn.microsoft.com/zh-/library/BR212474 (V = vs.94) .aspx, 
Then create a DataView object and pass ArrayBuffer. Then use the DataView’s setuint and getuint method to read settings, specifically refer to API ==> https://msdn.microsoft.com/zh-cn/LIBRARY/BR212463 (V=VS.94 ).ASPX

below is received:

// Receive message OnMessagesocket.onMessage = function (data) {var blob_ = new blob ([data.data]); Parseblob (blob_);} // Using FileReader Operation BLOB Object VAR Reader = {Readas: Function (Type, BLOB, CB) {var R = New fileReader (); r.onLoadEndnd = 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 bit is a VAR SocketCondype = dataView_.getuint8 (1) that determines the source of data; // Transferring a string read data var modulength = (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;});}}
  After the string can be, it can be for what you want.  
After reading this article, I believe that you have a certain understanding of JS direct operation of binary data. I want to know more related knowledge. Welcome to Tumi Cloud Industry Information Channel, thank you for reading!

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment