-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
116 lines (97 loc) · 2.71 KB
/
socket.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
const net = require('net');
const fs = require('fs');
class SocketClient {
constructor() {
this.connected = false;
this.client = null;
}
connect(host, port, handler, errorHandler) {
if (!host || host.trim() === '') {
return Promise.reject('请输入IP');
}
if (!port || port.trim() === '') {
return Promise.reject('请输入端口');
}
if (handler && typeof handler !== 'function') {
console.log('handler should be a function');
return;
}
if (this.client == null) {
this.client = new net.Socket();
}
let buf = new Buffer(0);
let json;
let bitmap;
let jsonLen = 0;
let imageLen = 0;
let promise;
promise = new Promise((resolve, reject) => {
this.client.connect(port, host, () => {
this.connected = true;
console.log('CONNECTED TO: ' + host + ':' + port);
resolve();
});
this.client.on('data', (data) => {
try {
buf = Buffer.concat([buf, data]);
if (jsonLen === 0) {
try {
jsonLen = buf.readInt32LE();
console.log('length:', jsonLen);
} catch(err) {
jsonLen = 0;
err.code = 1;
errorHandler && errorHandler(err);
}
}
if (jsonLen > 0 && buf.length >= 4 + jsonLen) {
json = buf.slice(4, 4 + jsonLen);
//json buffer
json = json.toString('ascii');
//parse json
try {
json = JSON.parse(json);
imageLen = json.image_size;
} catch(err) {
imageLen = 0;
err.code = 2;
errorHandler && errorHandler(err);
}
console.log('parse json:', json);
buf = buf.slice(4 + jsonLen);
jsonLen = -1;
}
if (jsonLen < 0 && imageLen > 0 && buf.length >= imageLen) {
bitmap = buf.slice(0, imageLen);
handler && handler(bitmap, json);
buf = buf.slice(imageLen);
jsonLen = 0;
imageLen = 0;
bitmap = null;
json = null;
}
} catch(err) {
console.log(err);
err.code = 3;
errorHandler && errorHandler(err);
}
});
this.client.on('error', (err) => {
err.code = 4;
errorHandler && errorHandler(err);
});
this.client.on('close', () => {
console.log('Connection closed');
});
});
return promise;
}
disconnect() {
if (this.client) {
this.client.pause();
this.client.destroy();
this.client = null;
}
}
}
module.exports = SocketClient;