index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. const app = require('express')();
  2. const dgram = require('dgram'); //UDP datagram socket
  3. const net = require('net'); //TCP net service
  4. const ProtoBuf = require('protobufjs');
  5. const server = require('http').Server(app);
  6. const WebSocket = require('ws');
  7. // const io = require('socket.io')(server, {
  8. // pingInterval: 10000,
  9. // pingTimeout: 5000,
  10. // cookie: false
  11. // });
  12. const wss = new WebSocket.Server({
  13. port: 3000,
  14. perMessageDeflate: {
  15. zlibDeflateOptions: { // See zlib defaults.
  16. chunkSize: 1024,
  17. memLevel: 7,
  18. level: 3,
  19. },
  20. zlibInflateOptions: {
  21. chunkSize: 10 * 1024
  22. },
  23. // Other options settable:
  24. clientNoContextTakeover: true, // Defaults to negotiated value.
  25. serverNoContextTakeover: true, // Defaults to negotiated value.
  26. clientMaxWindowBits: 10, // Defaults to negotiated value.
  27. serverMaxWindowBits: 10, // Defaults to negotiated value.
  28. // Below options specified as default values.
  29. concurrencyLimit: 10, // Limits zlib concurrency for perf.
  30. threshold: 1024, // Size (in bytes) below which messages
  31. // should not be compressed.
  32. }
  33. })
  34. wss.broadcast = function broadcast(data) {
  35. wss.clients.forEach(function each(client) {
  36. if (client.readyState === WebSocket.OPEN) {
  37. client.send(data);
  38. }
  39. });
  40. };
  41. wss.on('connection', ws => {
  42. ws.on('message', data => {
  43. decodeMsg(data, (msg) => {
  44. console.log('received: %s', JSON.stringify(msg));
  45. });
  46. })
  47. })
  48. app.get('/', function (req, res) {
  49. res.sendFile(__dirname + '/index.html');
  50. });
  51. app.get('/broadNotice', (req, res) => {
  52. let bufMsg = {
  53. "notificationMessage":{
  54. "nType": 1
  55. }
  56. }
  57. console.log("do broadNotice");
  58. encodeMsg(bufMsg, resBuffer => {
  59. wss.broadcast(resBuffer)
  60. res.send('1')
  61. })
  62. })
  63. app.get('/broadFriends', (req, res) => {
  64. let bufMsg = {
  65. "notificationMessage":{
  66. "nType": 2
  67. }
  68. }
  69. console.log("do broadFriends");
  70. encodeMsg(bufMsg, resBuffer => {
  71. wss.broadcast(resBuffer)
  72. res.send('1')
  73. })
  74. })
  75. app.get('/broadArtists', (req, res) => {
  76. let bufMsg = {
  77. "notificationMessage":{
  78. "nType": 3
  79. }
  80. }
  81. console.log("do broadArtists");
  82. encodeMsg(bufMsg, resBuffer => {
  83. wss.broadcast(resBuffer)
  84. res.send('1')
  85. })
  86. })
  87. const encodeMsg = function(resMsg, callback) {
  88. console.log(resMsg);
  89. ProtoBuf.load("./message.proto", (err, root) => {
  90. if(err) {
  91. throw err;
  92. }
  93. let Message = root.lookupType("tutorial.Message");
  94. let resBuffer = Message.encode(resMsg).finish()
  95. callback && callback(resBuffer)
  96. });
  97. }
  98. const decodeMsg = function(bufData, callback) {
  99. ProtoBuf.load("./message.proto", (err, root) => {
  100. if(err) {
  101. throw err;
  102. }
  103. let Message = root.lookupType("tutorial.Message");
  104. let msg = Message.decode(bufData);
  105. callback && callback(msg)
  106. });
  107. }
  108. /** TCP socket */
  109. // var TCPSocket = new net.Socket()
  110. // TCPSocket.connect(8108, '127.0.0.1', () => {
  111. // TCPSocket.write('Socket connected !');
  112. // })
  113. /** UDP socket */
  114. // var UDPSocket = dgram.createSocket({
  115. // type: "upd4",
  116. // fd: "8080"
  117. // }, (err, msg) => {
  118. // if(err) {
  119. // console.log(err);
  120. // }
  121. // console.log(msg);
  122. // })
  123. // UDPSocket.on("message", (msg, rinfo) => {
  124. // })
  125. /** socket.io */
  126. // io.on('connection', socket => {
  127. // socket.on('fuck', msg => {
  128. // socket.emit('getFuck', msg)
  129. // console.log('message: ', msg);
  130. // });
  131. // // socket.on('addFriends', () => {
  132. // // console.log('message: addFriends');
  133. // // let users = [{
  134. // // "gender": 1,
  135. // // "head": "https:\/\/wx.qlogo.cn\/mmopen\/vi_32\/vYcpJZR6j8lWXEO1ibFEgGhodFsyyUhKRKich3PeUZLZdDAvgMMNGhcT2E68UjKTquNplKQrmSKL0Eht1xiaNC7vg\/132",
  136. // // "isApplied": 0,
  137. // // "nick": "WhalewFall",
  138. // // "stars": 0,
  139. // // "uid": 120406
  140. // // }, {
  141. // // "gender": 1,
  142. // // "head": "\/\/wx.qlogo.cn\/mmopen\/y7Xfib97jgPdaNm8OtUnd8RmeEfaxbuJ4WV4PKrGU13u1Wr9ZugdFHGQnSL2ekINibLPkxibxSnsh2Qat9cYwCb35UuRSVZoUI7\/132",
  143. // // "isApplied": 0,
  144. // // "nick": "\u5927\u519B",
  145. // // "stars": 0,
  146. // // "uid": 66697
  147. // // }]
  148. // // socket.emit('onChangeFriends', users)
  149. // // })
  150. // // socket.on("message", data => {
  151. // // console.log(JSON.stringify(data));
  152. // // data = new Uint8Array(data);
  153. // // try {
  154. // // var msg = Message.decode(data);
  155. // // console.log("Received: ", msg);
  156. // // } catch (err) {
  157. // // console.log("Processing failed: ", err);
  158. // // }
  159. // // `1 `})
  160. // // setInterval(() => {
  161. // // socket.emit('help', {
  162. // // key : 'Help me !',
  163. // // value: 666
  164. // // })
  165. // // }, 2000);
  166. // });
  167. server.listen(3122, () => {
  168. console.log('listening on *:3122');
  169. });