File tree Expand file tree Collapse file tree 2 files changed +14
-2
lines changed
Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change 1919 bluetooth . connect ( address , channel , function ( err , connection ) {
2020 if ( err ) return console . error ( err ) ;
2121
22+ connection . delimiter = Buffer . from ( '\n' , 'utf8' ) ;
2223 connection . on ( 'data' , ( buffer ) => {
2324 console . log ( 'received message:' , buffer . toString ( ) ) ;
2425 } ) ;
Original file line number Diff line number Diff line change @@ -18,12 +18,23 @@ function Connection(port, address){
1818 const self = this ;
1919 this . port = port ;
2020 this . address = address ;
21+ this . buffer = new Buffer ( 0 ) ;
2122 const read = function ( ) {
2223 process . nextTick ( function ( ) {
2324 if ( self . isOpen ( ) ) {
24- self . port . read ( function ( err , data ) {
25+ self . port . read ( function ( err , chunk ) {
2526 if ( err ) return self . emit ( 'error' , err ) ;
26- self . emit ( 'data' , data ) ;
27+ if ( self . delimiter ) {
28+ let data = Buffer . concat ( [ self . buffer , chunk ] ) ;
29+ let position ;
30+ while ( ( position = data . indexOf ( self . delimiter ) ) !== - 1 ) {
31+ self . emit ( 'data' , data . slice ( 0 , position ) ) ;
32+ data = data . slice ( position + self . delimiter . length ) ;
33+ }
34+ self . buffer = data ;
35+ } else {
36+ self . emit ( 'data' , chunk ) ;
37+ }
2738 read ( ) ;
2839 } ) ;
2940 }
You can’t perform that action at this time.
0 commit comments