Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
Added encoding setting and conversion (#18)
Browse files Browse the repository at this point in the history
`tk102.settings.encoding = 'binary';`
  • Loading branch information
fvdm committed Sep 3, 2015
1 parent 36f2bd2 commit 1a6b8c6
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions tk102.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ License: Unlicense / Public Domain (see UNLICENSE file)

var net = require ('net');
var EventEmitter = require ('events') .EventEmitter;
var iconv = require ('iconv-lite');
var tk102 = new EventEmitter ();

// defaults
tk102.settings = {
ip: '0.0.0.0',
port: 0, // 0 = random, see `listening` event
connections: 10,
timeout: 10
timeout: 10,
encoding: 'utf8'
};


Expand Down Expand Up @@ -107,7 +109,9 @@ tk102.createServer = function (vars) {

// inbound connection
tk102.server.on ('connection', function (socket) {
socket.setEncoding ('binary');
if (tk102.settings.encoding !== 'utf8') {
socket.setEncoding ('binary');
}
tk102.emit ('connection', socket);
var data = [];
var size = 0;
Expand All @@ -119,7 +123,12 @@ tk102.createServer = function (vars) {
});

socket.on( 'close', function() {
data = Buffer.concat (data, size).toString ('utf8');
data = Buffer.concat (data, size);
if (tk102.settings.encoding !== 'utf8') {
data = iconv.decode (data, tk102.settings.encoding);
} else {
data = toString ('utf8');
}
var gps = {};
if (data != '') {
var gps = tk102.parse (data);
Expand Down

0 comments on commit 1a6b8c6

Please sign in to comment.