我正在构建一个SocketIO服务器,并且我已经设法通过HTTP实现它。但我想通过HTTPS使用它,这是我的问题。
我找不到任何关于如何操作的文档:它不工作或过时。
这是我的代码:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'pim',
port : 8889
});
var cors = require('cors');
var parse = require('url-parse');
var connected_users = [];
app.use(cors());
connection.connect();
http.listen(3000, function() {
console.log('Websockets server listening on port 3000');
setInterval(function() {
connected_users.forEach(function(user) {
connection.query('SELECT * FROM notification WHERE is_read = 0 AND user_id = ' + connection.escape(user.user_id), function(err, rows) {
if (rows.length > 0) {
rows.forEach(function(row) {
io.to(user.session_id).emit('notification', row);
});
/* Updating notification is treated after not to slow down the notification events */
rows.forEach(function(row) {
connection.query('UPDATE notification SET is_read = 1 WHERE id = ' + row.id + ' AND user_id = ' + connection.escape(user.user_id));
});
}
});
});
}, 5000);
});
io.sockets.on('connection', function(socket) {
var url = parse(socket.handshake.url, true);
var user_id = url.query.user_id;
var session_id = socket.id;
if (undefined !== user_id && 'null' !== user_id) {
connected_users.push({
session_id: session_id,
user_id: user_id
});
console.log('A user with ID ' + user_id + ' connected.');
}
socket.on('disconnect', function(socket) {
connected_users.forEach(function(user, index) {
if (user.session_id === session_id) {
connected_users.splice(index, 1);
}
});
console.log('A user with ID ' + user_id + ' disconnected.');
});
});
你能帮我解决一下这个问题吗?还是最新的教程?我找不到一个......