const app = require('express')(); const dgram = require('dgram'); //UDP datagram socket const net = require('net'); //TCP net service const ProtoBuf = require('protobufjs'); const server = require('http').Server(app); const WebSocket = require('ws'); // const io = require('socket.io')(server, { // pingInterval: 10000, // pingTimeout: 5000, // cookie: false // }); const wss = new WebSocket.Server({ port: 3000, perMessageDeflate: { zlibDeflateOptions: { // See zlib defaults. chunkSize: 1024, memLevel: 7, level: 3, }, zlibInflateOptions: { chunkSize: 10 * 1024 }, // Other options settable: clientNoContextTakeover: true, // Defaults to negotiated value. serverNoContextTakeover: true, // Defaults to negotiated value. clientMaxWindowBits: 10, // Defaults to negotiated value. serverMaxWindowBits: 10, // Defaults to negotiated value. // Below options specified as default values. concurrencyLimit: 10, // Limits zlib concurrency for perf. threshold: 1024, // Size (in bytes) below which messages // should not be compressed. } }) wss.broadcast = function broadcast(data) { wss.clients.forEach(function each(client) { if (client.readyState === WebSocket.OPEN) { client.send(data); } }); }; wss.on('connection', ws => { ws.on('message', data => { decodeMsg(data, (msg) => { console.log('received: %s', JSON.stringify(msg)); }); }) }) app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); }); app.get('/broadNotice', (req, res) => { let bufMsg = { "notificationMessage":{ "nType": 1 } } console.log("do broadNotice"); encodeMsg(bufMsg, resBuffer => { wss.broadcast(resBuffer) res.send('1') }) }) app.get('/broadFriends', (req, res) => { let bufMsg = { "notificationMessage":{ "nType": 2 } } console.log("do broadFriends"); encodeMsg(bufMsg, resBuffer => { wss.broadcast(resBuffer) res.send('1') }) }) app.get('/broadArtists', (req, res) => { let bufMsg = { "notificationMessage":{ "nType": 3 } } console.log("do broadArtists"); encodeMsg(bufMsg, resBuffer => { wss.broadcast(resBuffer) res.send('1') }) }) const encodeMsg = function(resMsg, callback) { console.log(resMsg); ProtoBuf.load("./message.proto", (err, root) => { if(err) { throw err; } let Message = root.lookupType("tutorial.Message"); let resBuffer = Message.encode(resMsg).finish() callback && callback(resBuffer) }); } const decodeMsg = function(bufData, callback) { ProtoBuf.load("./message.proto", (err, root) => { if(err) { throw err; } let Message = root.lookupType("tutorial.Message"); let msg = Message.decode(bufData); callback && callback(msg) }); } /** TCP socket */ // var TCPSocket = new net.Socket() // TCPSocket.connect(8108, '127.0.0.1', () => { // TCPSocket.write('Socket connected !'); // }) /** UDP socket */ // var UDPSocket = dgram.createSocket({ // type: "upd4", // fd: "8080" // }, (err, msg) => { // if(err) { // console.log(err); // } // console.log(msg); // }) // UDPSocket.on("message", (msg, rinfo) => { // }) /** socket.io */ // io.on('connection', socket => { // socket.on('fuck', msg => { // socket.emit('getFuck', msg) // console.log('message: ', msg); // }); // // socket.on('addFriends', () => { // // console.log('message: addFriends'); // // let users = [{ // // "gender": 1, // // "head": "https:\/\/wx.qlogo.cn\/mmopen\/vi_32\/vYcpJZR6j8lWXEO1ibFEgGhodFsyyUhKRKich3PeUZLZdDAvgMMNGhcT2E68UjKTquNplKQrmSKL0Eht1xiaNC7vg\/132", // // "isApplied": 0, // // "nick": "WhalewFall", // // "stars": 0, // // "uid": 120406 // // }, { // // "gender": 1, // // "head": "\/\/wx.qlogo.cn\/mmopen\/y7Xfib97jgPdaNm8OtUnd8RmeEfaxbuJ4WV4PKrGU13u1Wr9ZugdFHGQnSL2ekINibLPkxibxSnsh2Qat9cYwCb35UuRSVZoUI7\/132", // // "isApplied": 0, // // "nick": "\u5927\u519B", // // "stars": 0, // // "uid": 66697 // // }] // // socket.emit('onChangeFriends', users) // // }) // // socket.on("message", data => { // // console.log(JSON.stringify(data)); // // data = new Uint8Array(data); // // try { // // var msg = Message.decode(data); // // console.log("Received: ", msg); // // } catch (err) { // // console.log("Processing failed: ", err); // // } // // `1 `}) // // setInterval(() => { // // socket.emit('help', { // // key : 'Help me !', // // value: 666 // // }) // // }, 2000); // }); server.listen(3122, () => { console.log('listening on *:3122'); });