forked from bgaff/tcp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jstcp.js
167 lines (138 loc) · 4.62 KB
/
jstcp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Written By Brian Geffon
var TCPUtil = { };
TCPUtil.prepareForTransmit = JSON.stringify;
TCPUtil.cleanFromTransmit = JSON.parse;
var TCPProxy = function(socketio_client, encoding, nodelay) {
var sock_encoding = encoding || 'utf8';
var opt_nodelay = nodelay || false;
var net = require("net");
var socket = new net.Socket();
socket.setEncoding(sock_encoding);
socket.setNoDelay(opt_nodelay);
var client = socketio_client;
var sock_connected = false;
var socketio_connected = true; // tcp proxy can only be constructed
// after the socketio connection has been established
client.on("disconnect", function() {
// clean up the tcp connection if it's open.
socketio_connected = false;
if(sock_connected)
socket.destroy(); // since this wasn't a clean end.
sock_connected = false;
TCPUtil.log("socketio client disconnected.");
});
client.on("message", function(data) { // message from client came in
data = TCPUtil.cleanFromTransmit(data);
switch(data.action){
case "connect":
TCPUtil.log("connect request received: " + data.host + ":" + data.port);
socket.connect(data.port,data.host);
break;
case "disconnect":
if(sock_connected)
socket.end();
break;
case "data":
//var raw = data.data;
var raw = data.data;
if(data.encoding === 'base64') {
if(typeof(TCPUtil.base64.decode) === 'function'){
var base64decoded = TCPUtil.base64.decode(data.data);
raw = new Buffer(base64decoded, 'binary');
} else {
TCPUtil.log("ERROR: NO base64 decoder available.");
}
} else { raw = data.data; }
if(sock_connected)
socket.write(raw);
break;
default:
break;
}
});
socket.on("end", function() {
sock_connected = false;
if(socketio_connected)
client.send(TCPUtil.prepareForTransmit({action: "closed"}));
});
socket.on("connect", function(){
sock_connected = true;
TCPUtil.log("socket connected");
if(socketio_connected)
client.send(TCPUtil.prepareForTransmit({action: "connected"}));
});
socket.on("data", function(sck_data) {
TCPUtil.log("data arrived:" + sck_data + ", length: " + sck_data.length );
if(socketio_connected)
client.send(TCPUtil.prepareForTransmit({action: "data", encoding: sock_encoding, data: sck_data}));
});
};
var TCPClient = function(host, port) {
this.host = host;
this.port = port;
this._connected_to_socket = false; // are we connected to socketio
this._connected = false; // do we have a connection to the TCP endpoint
this._callbacks = {};
return this;
};
TCPClient.prototype.emit = function(event, param) {
if(typeof this._callbacks[event] === 'function')
this._callbacks[event].call(this, param);
};
TCPClient.prototype.on = function(event, callback) {
if(typeof callback === 'function')
this._callbacks[event] = callback;
return this;
};
TCPClient.prototype.disconnect = function() {
if(this._connected_to_socket)
this._socket.send(TCPUtil.prepareForTransmit({action: "disconnect"}));
};
TCPClient.prototype.send = function(senddata, encoding){
var data_encoding = encoding || 'utf8';
if(this._connected_to_socket && this._connected){
this._socket.send(TCPUtil.prepareForTransmit({action: "data", encoding: data_encoding, data: senddata}));
}
}
TCPClient.prototype.connect = function() {
var that = this;
// FIXME: there is currently a bug with reconnecting
// after a TCP connection has been closed, this
// version currently does not support it...
if(typeof this._socket === "undefined" || this._socket === null)
this._socket = io.connect();
this._socket.on("connect", function(){
that._connected_to_socket = true;
that._socket.send(TCPUtil.prepareForTransmit({action: "connect", host: that.host, port: that.port}));
});
this._socket.on('disconnect', function() {
that._connected_to_socket = false;
that._connected = false;
that.emit("error", "The socket io connection was lost");
});
this._socket.on("message", function(data){
data = TCPUtil.cleanFromTransmit(data);
switch(data.action){
case "connected":
that._connected = true;
that.emit("connected");
break;
case "data":
that.emit("data", {encoding: data.encoding, data: data.data});
break;
case "closed":
that._connected = false;
that.emit("closed");
default:
}
});
return this;
};
if (typeof exports !== "undefined" && exports !== null) {
module.exports = TCPProxy; // we only need to expose TCPProxy to node.js
// using a TCPClient wrapper in node would not make any sense.
TCPUtil.base64 = require('base64');
TCPUtil.log = function() { }; // supress logging
} else {
TCPUtil.log = function() { }; //console.log;
}