Recently done a few projects, with JS operation binary data, through the socket and the background for transmission. Use a blog to make a record here
The first is to create a new socket:
1 var socket=New WebSocket ("ws://192.168.0.147");
Then define the function that the socket opens and executes after the connection:
WebSocket has a property binarytype, which can be set to "blob" or "Arraybuffer", the default format is "Blob", when the project is forgotten to set to "Arraybuffer", The result is that the Blob object is needed to receive the data below.
socket.onopen=function() { // Send login frame, 4-20 bits for mobile number var loginarr=[ 0x02,0x02,0x00,0x1e,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x0d,0x0a] }
Here are the Bype sent out:
varLoginbuffer=NewArrayBuffer (30);varlogindataview=NewDataView (loginbuffer);//localstorageuserinfo for cached local user phone numbervarTelarr=Localstorageuserinfo. Telphone; varLogintime=Temptrans (); for(vari=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 Packagesocket.send (Logindataview.buffer); //format time in accordance with the two digits of the month of the second 1-bit by the high end of the sortfunctionTemptrans (time) { if(!Time ) { time=NewDate (); } varu32dataview=NewDataView (NewUint16array ([Time.getfullyear ()]). Buffer); varuint8=[]; Uint8.push (NewDataView (NewUint8array ([0x00]). Buffer). getUint8 (0)) for(vari=u32dataview.bytelength-1;i>=0;i--) {Uint8.push (U32dataview.getuint8 (i))} Uint8.push (NewDataView (NewUint8array ([Time.getmonth () +1]). Buffer). getUint8 (0)); Uint8.push (NewDataView (NewUint8array ([Time.getdate ()]). Buffer). getUint8 (0)); Uint8.push (NewDataView (NewUint8array ([Time.gethours ()]). Buffer). getUint8 (0)); Uint8.push (NewDataView (NewUint8array ([Time.getminutes ()]). Buffer). getUint8 (0)); Uint8.push (NewDataView (NewUint8array ([Time.getseconds ()]). Buffer). getUint8 (0)); returnuint8;}
The process of sending is probably the case, first the new Arraybuffer object, which needs to fill in the buffer length parameter, see api==> https://msdn.microsoft.com/zh-cn/library/br212474 (v= vs.94). aspx,
Then create a new DataView object and pass the Arraybuffer in. Then use the DataView Setuint and Getuint method to read the settings in bits, the specific reference api==> https://msdn.microsoft.com/zh-cn/library/br212463 (v=vs.94). aspx
Here is the receive data processing:
1 //Receive Message OnMessage2Socket.onmessage=function(data) {3 varblob_=NewBlob ([Data.data]);4 Parseblob (blob_);5 }6 //manipulating Blob objects with FileReader7 varReader = { 8Readas:function(TYPE,BLOB,CB) {9 varR =NewFileReader ();TenR.onloadend =function(){ One if(typeof(CB) = = = ' function ') { A Cb.call (r,r.result); - } - } the Try{ -r[' Readas ' +Type] (BLOB); -}Catch(e) {} - } + } - functionParseblob (BLOB) { +Reader.readas (' ArrayBuffer ', Blob.slice (0,blob.size),function(arr) { A vardataview_=NewDataView (arr); at //The second place in the protocol is to judge the source of the data - varSocketcontype=dataview_.getuint8 (1); - //go to String to read data - varModulelength= (dataview_.buffer.bytelength-46)/33; - varModulestate={}; -Reader.readas (' Text ', Blob.slice (i*33+37,i*33+37+32),function(Result) { inModulestate[dataview_.getuint8 (i*33+36)]=result; - }); to }) +}
After turning into a string, you can do whatever you like.
The above is what I used to do the project of the operation of the binary data method, the bit read head will be fried ... Google Open source protobuf can be set up after the data format, all the reading operation without their own stitching, very comfortable. But I do not know the background technology, I can only follow the back of the big guy to eat steamed slag TnT
The first time to write a blog, I hope that you can more guidance communication
JS manipulating binary data