我为所有登录用户提供了一个唯一的令牌,现在我想在第三方服务器连接到我的套接字后找到令牌数组的令牌(如果该令牌可用)我必须通过点击Web服务获取文本消息我必须使用套接字发射函数向该用户发送数据哪个令牌匹配,但我也需要该用户套接字ID。如何找到每个登录用户的套接字ID? 并使用令牌将消息发送到该套接字ID。
var bodyParser = require("body-parser");
var bcrypt = require('bcrypt');
var urlEncodedParser = bodyParser.urlencoded({extended: false});
var session = require('client-sessions');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var request = require('request');
var utils = require('utility');
app.use(bodyParser.text());
var token_array = new Array();
require('array.prototype.find');
app.use(session({
cookieName: 'usersession',
secret: '0GB2ZbiKBtz2flRPvlJZ9EtczCewBxXK',
duration: 24 * 60 * 60 * 1000, // how long the session will stay valid in ms
activeDuration: 1000 * 60 * 5
}));
............................................... .........................
app.post("/login", urlEncodedParser, function(req, response)
{
var body = req.body;
console.log(req.body);
var userData={
"deviceToken" : "AbcDE1234567",
"password" : body.pwd,
"username" : body.username
};
var digest = utils.hmac("sha1", "A12AA418-1F28-4464-8B67-29CBD02BC45C-F048B14F-F3E3-4F97-A522-F2275A364A0E", JSON.stringify(userData));
var postData = {
"deviceToken" : "AbcDE1234567",
"password" : body.pwd,
"username" : body.username,
"digest" : digest
};
var sPostData = utils.base64encode(utils.base64encode(JSON.stringify(postData)));
request({
url: "http://201.196.6.218:8080/BACKSTAFF/service/authenticate/",
method: "POST",
headers: {
"content-type": "application/json",
},
body: sPostData
},
function(err,result,body){
//console.log(JSON.stringify(postData));
var body = JSON.parse(body);
// console.log(typeof body);
//console.log(body.token);
req.usersession.token = body.token;
token_array.push(body.token);
req.usersession.alltoken= token_array
//console.log(token_array);
response.redirect(301, '/contacts');
});
});
io.on('connection', function(socket){
socket.on('push', function(msg_id,token){
console.log(token_array);
// console.log(token_array.find('244d6575-fc60-4815-a331-274a24d65a95'));
Array.prototype.contains = function(elem)
{
for (var i in this)
{
if (this[i] == elem) return true;
}
return false;
}
if (token_array.contains('66a1fd91-86d6-4884-b483-167ca5ff005f'))
{
var msgData={
"messageID" : msg_id,
"token" : token
};
var digest = utils.hmac("sha1", "A12AA418-1F28-4464-8B67-29CBD02BC45C-F048B14F-F3E3-4F97-A522-F2275A364A0E", JSON.stringify(userData));
var msgPostData = {
"token" : token,
"messageID" : msg_id,
"digest" :digest
};
var msgencodeData = utils.base64encode(utils.base64encode(JSON.stringify(postData)));
request({
url: "http://201.196.6.218:8080/BACKSTAFF/service/getTextMessage/",
method: "POST",
headers: {
"content-type": "application/json",
},
body: msgencodeData
},
function(err,result,body){
//console.log(JSON.stringify(postData));
var body = JSON.parse(body);
var message = body.message;
socket.emit('push', message);
});
}
else
{
console.log('no');
}
// io.emit('chat message', msg);
});
});
............................................... ...............
答案 0 :(得分:0)
如果要跟踪套接字(例如,通过令牌),则应将它们保存在对象中。因此,当客户端连接时,您将套接字保存在对象中,如下所示:
var clientObj = {};
io.on('connection', function(socket){
clientObj[token] = socket;
...
}
然后你可以像这样发射到套接字:
io.to(clientObj[token]).emit('something', 'some message');