这更多是软件体系结构问题,而不是特定的编码问题。
我正在编写一个ios应用程序,该应用程序依赖于将消息和事件即时传递到订阅该服务的其他移动设备。为此,我已经为此邀请了socket.io运行在node.js后端服务器上。
下一步是用户身份验证系统,我希望用户可以注册和登录后才能使用该服务。
我想就实现该功能的最佳方法提出建议。我看到的选项是:
我主要关心的是保持应用程序的实时延迟并确保安全性。 假设一切都通过TLS完成
答案 0 :(得分:0)
当然,您可以使用socket.io并表达这些东西。
在这里,我想包含一个链接。在哪里可以看到您可以多么简单地构建实时应用程序。
Simple real-time application using socket-io
服务器端:
//-----------------Dependencies------------------
var app = require("express")();
var pgsql = require("pg");
var http = require('http').Server(app);
var io = require("socket.io")(http);
//------------------------------------------------
const con = new pgsql.Pool({
user: 'postgres',
host: 'localhost',
database: 'fbstatus',
password: 'postgres',
port: '5432'
});
app.get("/",function(req,res){
res.sendFile(__dirname + '/index.html');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
// Connect to Postgres
con.connect(function(err, client) {
if(err) {
console.log(err);
}
io.sockets.on('connection', function (socket) {
// Listen for all pg_notify channel messages
client.on('notification', function(msg) {
console.log(msg);
socket.emit('update', msg);
});
// Designate which channels we are listening on. Add additional
channels with multiple lines.
client.query('LISTEN notification_events');
});
});
客户端:-
<!DOCTYPE html>
<html>
<body>
<h1>real time data recieved from server</h1>
<p>When you insert a new row into database, the below p tag
content will be changed</p>
<p id="socketio"></p>
</body>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('update', function (data) {
var data_got=JSON.parse(data.payload)
console.log(data_got["data"]["id"]);
document.getElementById("socketio").innerHTML = data.payload;
});
</script>
</html>