我们提供统一消息系统招投标所需全套资料,包括统一消息系统介绍PPT、统一消息系统产品解决方案、
统一消息系统产品技术参数,以及对应的标书参考文件,详请联系客服。
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('Hello! You are now connected.');
});
]]>
const mysql = require('mysql');
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'message_service'
});
db.connect();
function saveMessage(message) {
const query = `INSERT INTO messages (content, userId) VALUES ('${message}', 1)`;
db.query(query, (err, result) => {
if (err) throw err;
console.log(`Saved message: ${message}`);
});
}
]]>
app.get('/messages/:userId', (req, res) => {
const userId = req.params.userId;
const query = `SELECT * FROM messages WHERE userId=${userId} ORDER BY createdAt DESC LIMIT 10 OFFSET ${req.query.offset || 0}`;
db.query(query, (err, results) => {
if (err) return res.status(500).send(err);
res.json(results);
});
});
]]>