|
| 1 | +var net = require("net"); |
| 2 | +var ExBuffer = require("./lib/ExBuffer"); |
| 3 | + |
| 4 | +var client = null; |
| 5 | +var exBuffer = null; |
| 6 | +var connectCallback = null; |
| 7 | +var receiveCallback = null; |
| 8 | +var closeCallback = null; |
| 9 | +var serverPort = null; |
| 10 | +var serverHost = null; |
| 11 | +var hasShutdown = false; |
| 12 | +var debuger = console.log; |
| 13 | + |
| 14 | +function connect(port, host, onConnected, onReceived, onClosed){ |
| 15 | + close(false); |
| 16 | + |
| 17 | + if (port){ |
| 18 | + serverPort = port; |
| 19 | + } |
| 20 | + if (host){ |
| 21 | + serverHost = host; |
| 22 | + } |
| 23 | + if (onConnected){ |
| 24 | + connectCallback = onConnected; |
| 25 | + } |
| 26 | + if (onReceived){ |
| 27 | + receiveCallback = onReceived; |
| 28 | + } |
| 29 | + if (onClosed){ |
| 30 | + closeCallback = onClosed; |
| 31 | + } |
| 32 | + |
| 33 | + client = net.connect(serverPort, serverHost, function(){ |
| 34 | + debuger('Client connected: ' + serverHost + ":" + serverPort); |
| 35 | + |
| 36 | + if (typeof connectCallback === "function"){ |
| 37 | + connectCallback(); |
| 38 | + } |
| 39 | + |
| 40 | + exBuffer = new ExBuffer().int8Tag().uint32Head().bigEndian(); |
| 41 | + exBuffer.on('data', onReceivePackData); |
| 42 | + |
| 43 | + client.on('data', function(data) { |
| 44 | + exBuffer.put(data); |
| 45 | + }); |
| 46 | + |
| 47 | + client.on('end', function() { |
| 48 | + debuger('Client disconnected from server'); |
| 49 | + if (typeof closeCallback === "function"){ |
| 50 | + closeCallback(); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + client.on('timeout', function() { |
| 55 | + debuger('The socket times out.'); |
| 56 | + //setTimeout(reconnect, 1000); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + client.on('error', function(error){ |
| 61 | + debuger("The socket had an error: " + error.code); |
| 62 | + setTimeout(reconnect, 1000); |
| 63 | + }); |
| 64 | + |
| 65 | + client.on('close', function() { |
| 66 | + debuger('The socket closed.'); |
| 67 | + }); |
| 68 | + |
| 69 | + //client.setTimeout(15000); |
| 70 | +} |
| 71 | + |
| 72 | +function reconnect(){ |
| 73 | + if (!hasShutdown){ |
| 74 | + connect(); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function close(shutdown = true) { |
| 79 | + hasShutdown = shutdown; |
| 80 | + if (client){ |
| 81 | + client.end(); |
| 82 | + client.destroy(); |
| 83 | + client = null; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function send(cmd, arg){ |
| 88 | + debuger('[Send] tag: ' + cmd + ' value: ' + (arg ? JSON.stringify(arg) : '')); |
| 89 | + if (client){ |
| 90 | + var data = null, len = 0; |
| 91 | + if (arg){ |
| 92 | + data = JSON.stringify(arg); |
| 93 | + len = Buffer.byteLength(data); |
| 94 | + } |
| 95 | + |
| 96 | + //写入1个字节的tag |
| 97 | + var tagBuf = new Buffer(1); |
| 98 | + tagBuf.writeInt8(cmd, 0); |
| 99 | + client.write(tagBuf); |
| 100 | + |
| 101 | + //写入4个字节的length |
| 102 | + var headBuf = new Buffer(4); |
| 103 | + headBuf.writeUInt32BE(len, 0); |
| 104 | + client.write(headBuf); |
| 105 | + |
| 106 | + //写入value |
| 107 | + if (len > 0){ |
| 108 | + var bodyBuf = new Buffer(len); |
| 109 | + bodyBuf.write(data); |
| 110 | + client.write(bodyBuf); |
| 111 | + } |
| 112 | + } else { |
| 113 | + debuger("socket 未连接"); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +function onReceivePackData(data){ |
| 118 | + var tag = data.tag; |
| 119 | + var value = data.value ? data.value.toString() : null; |
| 120 | + debuger('[Received] tag: ' + tag + ' value: ' + value); |
| 121 | + |
| 122 | + if (value){ |
| 123 | + value = JSON.parse(value); |
| 124 | + } |
| 125 | + |
| 126 | + if (typeof receiveCallback === "function"){ |
| 127 | + receiveCallback(tag, value); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +function setDebuger(value){ |
| 132 | + debuger = value; |
| 133 | +} |
| 134 | + |
| 135 | +module.exports.setDebuger = setDebuger; |
| 136 | +module.exports.connect = connect; |
| 137 | +module.exports.close = close; |
| 138 | +module.exports.send = send; |
0 commit comments