wechat wss demo

wss-server-6.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. 'use strict';
  2. const encoding = require("encoding");
  3. const zlib = require('zlib');
  4. function stringToArray(bufferString) {
  5. // let uint8Array = new TextEncoder("utf-8").encode(bufferString);
  6. //var resultBuffer = encoding.convert(nameString, 'ASCII', 'UTF-8');
  7. var uint8Array = encoding.convert(bufferString, 'ASCII', 'UTF-8');
  8. return uint8Array;
  9. }
  10. function getDateTime() {
  11. var date = new Date();
  12. var hour = date.getHours();
  13. hour = (hour < 10 ? "0" : "") + hour;
  14. var min = date.getMinutes();
  15. min = (min < 10 ? "0" : "") + min;
  16. var sec = date.getSeconds();
  17. sec = (sec < 10 ? "0" : "") + sec;
  18. var year = date.getFullYear();
  19. var month = date.getMonth() + 1;
  20. month = (month < 10 ? "0" : "") + month;
  21. var day = date.getDate();
  22. day = (day < 10 ? "0" : "") + day;
  23. return year + ":" + month + ":" + day + ":" + hour + ":" + min + ":" + sec;
  24. }
  25. function intToUint8Array(value) {
  26. console.log('value:' + value);
  27. var arr = new Uint8Array(2);
  28. arr[0] = (value >> 8);
  29. arr[1] = value % 0x100;
  30. return arr;
  31. }
  32. function intToUint8ArrayCRC(value) {
  33. console.log('value:' + value);
  34. var arr = new Uint8Array(2);
  35. arr[0] = value % 0x100;
  36. arr[1] = (value >> 8);
  37. return arr;
  38. }
  39. function calc_crc16(data, len) {
  40. var crc = 0xffff;
  41. var LSB;
  42. for (var i = 0; i < len; i++) {
  43. crc ^= data[i];
  44. for (var j = 0; j < 8; j++) {
  45. LSB = crc & 1;
  46. crc = crc >> 1;
  47. if (LSB != 0) crc ^= 0xa001;
  48. }
  49. }
  50. return crc;
  51. }
  52. //wss start
  53. var fs = require('fs');
  54. var cfg = {
  55. ssl: true,
  56. port: 443,
  57. ssl_key: 'ssl.key', ssl_cert: 'ssl.crt'
  58. };
  59. var httpServ = (cfg.ssl) ? require('https') : require('http');
  60. const WebSocketServer = require('ws').Server;
  61. var app = null;
  62. var processRequest = function(req, res)
  63. {
  64. res.writeHead(200);
  65. res.end('All glory to WebSockets!\n');
  66. };
  67. if (cfg.ssl) {
  68. app = httpServ.createServer({
  69. key: fs.readFileSync(cfg.ssl_key),
  70. cert: fs.readFileSync(cfg.ssl_cert)
  71. }, processRequest).listen(cfg.port);
  72. } else {
  73. app = httpServ.createServer(processRequest).listen(cfg.port);
  74. }
  75. const wss = new WebSocketServer({ server: app });
  76. wss.on('connection', function(wsConnect)
  77. {
  78. //0
  79. //
  80. //mqtt
  81. var mqtt = require('mqtt');
  82. var opt = {
  83. port:1883,
  84. clientId: 'phonetest'
  85. };
  86. function uint8ArrayToInt(arr) {
  87. console.log('arr[0]:' + arr[0]);
  88. console.log('arr[1]:' + arr[1]);
  89. return arr[0] * 0x100 + arr[1];
  90. }
  91. var client = mqtt.connect('tcp://iot.twgigatech.cn:1883');
  92. client.on('connect', function ()
  93. {
  94. //fs.appendFile('./log/wss_conn.log',getDateTime()+' ' +'wss connect in'+'\r\n');
  95. //fs.appendFile('./log/all.log',getDateTime()+' ' +'wss connect in \r\n');
  96. console.log('connect MQTT server');
  97. //client.subscribe("CAD9A3D3EUWWNBKY111A/iotk");
  98. client.subscribe("CAD9A3D3EUWWNBKY111A/wxxk/#");
  99. client.subscribe("CAD9A3D3EUWWNBKY111A/wxxm/#");
  100. /*
  101. client.subscribe("CAD9A3D3EUWWNBKY111A/wxxm/5");
  102. client.subscribe("CAD9A3D3EUWWNBKY111A/wxxm/6");
  103. client.subscribe("CAD9A3D3EUWWNBKY111A/wxxm/7");
  104. client.subscribe("CAD9A3D3EUWWNBKY111A/wxxm/4");
  105. client.subscribe("CAD9A3D3EUWWNBKY111A/wxxm/3");
  106. client.subscribe("CAD9A3D3EUWWNBKY111A/wxxm/2");
  107. client.subscribe("CAD9A3D3EUWWNBKY111A/wxxm/1");
  108. client.subscribe("CAD9A3D3EUWWNBKY111A/wxxm/8");
  109. */
  110. // client.publish('CAD9A3D3EUWWNBKY/iotk','test');
  111. });
  112. client.on('message', function (topic, message) {
  113. console.log(' ');
  114. console.log(' topic = '+topic.toString());
  115. console.log('==============mqtt rece start ');
  116. console.log('mqtt rece: '+message.toString());
  117. console.log('=======================');
  118. //fs.appendFile('./log/all.log',getDateTime()+' ' +'mqtt_rece: '+message.toString()+'\r\n');
  119. //fs.appendFile('./log/mqtt_rece.log',getDateTime()+' ' +'mqtt_rece: '+message.toString()+'\r\n');
  120. console.log('messagelen:' + message.length.toString());
  121. let lenBin = message.subarray(26, 28);
  122. let len = uint8ArrayToInt(lenBin);
  123. console.log('len:' + len);
  124. let blockData = message.subarray(32, message.length);
  125. console.log('blockData:' + blockData);
  126. let dataType = blockData[0];
  127. console.log('dataType:' + dataType);
  128. var rawData = blockData.subarray(25, len-2);
  129. console.log('rawData:' + rawData);
  130. console.log('rawData len:' + rawData.length.toString());
  131. var strData;
  132. if(dataType == 0)
  133. {
  134. strData = arrayToString(rawData);
  135. } else if (dataType == 1)
  136. {
  137. // strData = zlib.inflate(rawData, function(err, buffer)
  138. zlib.inflate(rawData, function(err, buffer)
  139. {
  140. if (!err)
  141. {
  142. console.log('buffer= '+buffer);
  143. //fs.appendFile('./log/all.log',getDateTime()+' ' +'buffer: '+buffer+'\r\n');
  144. // buffer.length.toString();
  145. console.log('mqtt inflate OK ');
  146. console.log('wss.clients size~~~~~~~ '+ wss.clients.size);
  147. //fs.appendFile('./log/all.log',getDateTime()+' ' +'wss client size: '+wss.clients.size+'\r\n');
  148. //wss.send(buffer);
  149. //wssclients[0].send(buffer);
  150. var jsdata = buffer.toString();
  151. if (topic.toString().indexOf('wxxm')!=-1)
  152. {
  153. console.log(' wxxm find ');
  154. var jssdata = {
  155. "cmd": "wxxIotM"//,
  156. //"data":""
  157. };
  158. //jssdata['data']=jsdata ;
  159. jssdata['data']=JSON.parse(jsdata) ;
  160. try
  161. {
  162. //fs.appendFile('./log/all.log',getDateTime()+' ' +'wss_tomobile wxxIotM: '+JSON.stringify(jssdata )+'\r\n' );
  163. wsConnect.send(JSON.stringify(jssdata ));
  164. //fs.appendFile('./log/wss_tomobile.log',getDateTime()+' ' +'wss_tomobile wxxIotM: '+JSON.stringify(jssdata )+'\r\n');
  165. }
  166. catch(e)
  167. {
  168. console.log(e)
  169. }
  170. console.log(JSON.stringify(jssdata ));
  171. }
  172. if (topic.toString().indexOf('wxxk')!=-1)
  173. {
  174. console.log(' wxxk find');
  175. var jssdata = {
  176. "cmd": "wxxIotK"//,
  177. //"data":""
  178. };
  179. //jssdata['data']=jsdata ;
  180. jssdata['data']=JSON.parse(jsdata) ;
  181. try
  182. {
  183. //fs.appendFile('./log/all.log',getDateTime()+' ' +'wss_tomobile wxxIotK: '+jssdata+'\r\n' );
  184. wsConnect.send(JSON.stringify(jssdata ));
  185. //fs.appendFile('./log/wss_tomobile.log',getDateTime()+' ' +'wss_tomobile wxxIotK: '+jssdata +'\r\n');
  186. }
  187. catch(e)
  188. {
  189. console.log(e)
  190. }
  191. console.log(JSON.stringify(jssdata ));
  192. }
  193. console.log(' wssclients[0].send ');
  194. }
  195. });
  196. //number = 5
  197. } else {
  198. strData = '';
  199. }
  200. console.log('============mqtt rece finish');
  201. console.log(' ');
  202. //wsConnect.send(Buffer.from(message));
  203. // client.end();
  204. });
  205. //
  206. //0
  207. wsConnect.on('message', function(message)
  208. {
  209. console.log('wss rece: '+message);
  210. //to k or m
  211. //fs.appendFile('./log/wss_recemsg.log',getDateTime()+' ' +'wss rece: '+message+'\r\n');
  212. //fs.appendFile('./log/all.log',getDateTime()+' ' +'wss rece: '+message+'\r\n');
  213. var obj = JSON.parse(message.toString());
  214. var vdata =obj ['data'];
  215. console.log( 'obj.data:' + vdata);
  216. console.log( '~~~~~~~~~~~~~~~~~~~~~');
  217. message = vdata ;
  218. var rawData;
  219. var dataType;
  220. var event = 0;
  221. var bufferID = stringToArray('GA300_1_A1001001_5');
  222. console.log('message.length:' + message.length);
  223. try
  224. {
  225. if (message.length < 128) {
  226. dataType = 0;
  227. rawData = stringToArray(message );
  228. console.log('rawData:' + rawData);
  229. var blockData = new Uint8Array(25 + rawData.length + 2);
  230. blockData[0] = dataType;
  231. blockData.set(bufferID,1);
  232. blockData.set(rawData, 25);
  233. var crc16 = calc_crc16(blockData, 25 + rawData.length)
  234. blockData.set(intToUint8ArrayCRC(crc16), 25 + rawData.length);
  235. console.log('blockData: ' + blockData);
  236. var packedData = new Uint8Array(32 + blockData.length);
  237. packedData.set(bufferID);
  238. packedData.set(intToUint8Array(event), 24);
  239. packedData.set(intToUint8Array(25+rawData.length+2), 26);
  240. packedData.set(intToUint8Array(0), 28);
  241. packedData[30] = 1;
  242. packedData[31] = 1;
  243. packedData.set(blockData, 32);
  244. console.log('~~~~~~~~~~~~~~~~~~');
  245. console.log('~~~~~~~~~~~~~~~~~~');
  246. console.log('~~~~~~~~~~~~~~~~~~');
  247. client.publish('CAD9A3D3EUWWNBKY111A/iotm/5',Buffer.from(packedData));
  248. client.publish('CAD9A3D3EUWWNBKY111A/iotk/5',Buffer.from(packedData));
  249. //fs.File('./log/mqtt_puablish.log',getDateTime()+' ' +'mqtt publish: '+packedData+'\r\n');
  250. //fs.appendFile('./log/all.log',getDateTime()+' ' +'mqtt publish: '+packedData+'\r\n');
  251. console.log('~~~~~~~~~~~~~~~~~~');
  252. } else {
  253. dataType = 1;
  254. zlib.deflate(message, (err, buffer) => {
  255. try
  256. {
  257. if (!err) {
  258. console.log('deflate OK'+buffer.toString('base64'));
  259. console.log('rawData:' + buffer);
  260. rawData=buffer;
  261. var blockData = new Uint8Array(25 + rawData.length + 2);
  262. blockData[0] = dataType;
  263. blockData.set(bufferID,1);
  264. blockData.set(rawData, 25);
  265. var crc16 = calc_crc16(blockData, 25 + rawData.length)
  266. blockData.set(intToUint8ArrayCRC(crc16), 25 + rawData.length);
  267. console.log('blockData: ' + blockData);
  268. var packedData = new Uint8Array(32 + blockData.length);
  269. packedData.set(bufferID);
  270. packedData.set(intToUint8Array(event), 24);
  271. packedData.set(intToUint8Array(25+rawData.length+2), 26);
  272. packedData.set(intToUint8Array(0), 28);
  273. packedData[30] = 1;
  274. packedData[31] = 1;
  275. packedData.set(blockData, 32);
  276. console.log('~~~~~~~~~~~~~~~~~~');
  277. console.log('~~~~~~~~~~~~~~~~~~');
  278. console.log('~~~~~~~~~~~~~~~~~~');
  279. client.publish('CAD9A3D3EUWWNBKY111A/iotm/5',Buffer.from(packedData));
  280. client.publish('CAD9A3D3EUWWNBKY111A/iotk/5',Buffer.from(packedData));
  281. //fs.appendFile('./log/mqtt_puablish.log',getDateTime()+' ' +'mqtt publish: '+packedData+'\r\n');
  282. //fs.appendFile('./log/all.log',getDateTime()+' ' +'mqtt publish: '+packedData+'\r\n');
  283. console.log('~~~~~~~~~~~~~~~~~~');
  284. }
  285. }
  286. catch(e){
  287. console.log(e)
  288. }
  289. });
  290. }
  291. }
  292. catch(e){
  293. console.log(e)
  294. }
  295. //client.publish('CAD9A3D3EUWWNBKY111A/iotm/5',rawData);
  296. //client.publish('CAD9A3D3EUWWNBKY111A/iotk/5',rawData);
  297. //client.subscribe("CAD9A3D3EUWWNBKY111A/wxxm/#");
  298. //wsConnect.send(message);
  299. });
  300. });
  301. //wss finish
  302. //mqtt2
  303. var mqtt2 = require('mqtt');
  304. var client2 = mqtt2.connect('tcp://iot.twgigatech.cn:1883');
  305. client2.on('connect', function ()
  306. {
  307. //fs.appendFile('./log/wss_conn.log',getDateTime()+' ' +'wss connect in'+'\r\n');
  308. //fs.appendFile('./log/all.log',getDateTime()+' ' +'wss connect in \r\n');
  309. console.log('connect MQTT server');
  310. });
  311. client2.on('message', function (topic, message) {
  312. });
  313. //0
  314. //0