当我导航到我网站的子页面时,我的socket.io服务器正在运行索引页面的连接模块。
这是导航到www.example.com/query
时的完整控制台日志authenticated updated ==> { GbLZ5jxHz0S5uyNNAAAA: { user: 'yrn1jro2fw1nk4dyy4', group: 'uehw1o2grq1oy11y9xrgyw' } }
client connected to INDEX with id GbLZ5jxHz0S5uyNNAAAA
preloading index
client connected to QUERY with id GbLZ5jxHz0S5uyNNAAAA
preloading query
我的主要问题涉及我获得第二和第三行。我希望连接到“QUERY”而不是“INDEX”。
当我导航到www.example.com时,控制台会通过单个连接按预期打印。
这是我的整个server.js文件,我刚刚简化并运行以生成上面的简化控制台日志。您可以忽略身份验证的内容,我主要将其包含在那些可能会觉得有用的人身上。
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var util = require("./js/util.js");
var authenticated = {}; // [socket_id:{user:000,group:000},socket_id:{user:000,group:000}] // stores currently authenticated sockets
server.listen(80);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/query', function (req, res) {
res.sendFile(__dirname + '/query.html');
});
app.get('/favicon.ico', function (req, res) {
res.sendFile(__dirname + '/favicon.ico');
});
app.use("/js", express.static(__dirname + '/js'));
app.use("/css", express.static(__dirname + '/css'));
app.use("/images", express.static(__dirname + '/images'));
var mysql = require('mysql');
var db = mysql.createConnection({host:'localhost',user:'root',password:'fakepw',database:'baseofdata'});
db.connect();
var index = io.of('/').on('connection', function (socket) {
// console.log(socket);
console.log("client connected to INDEX with id",socket.id);
socket.on("authenticate",function(data){
console.log("authenticate data ==> ",data);
db.query("SELECT user,usergroup FROM group_users INNER JOIN users ON group_users.user = users.id WHERE username = ? AND password = ?",[data.user,data.pass],function(err, rows) {
console.log("auth returned ==> ",rows,err);
if (err === null && rows.length > 0)
{
authenticated[socket.id] = {user:rows[0].user,group:rows[0].usergroup};
encoded = {};
encoded.user = util.encode(rows[0].user);
encoded.usergroup = util.encode(rows[0].usergroup);
socket.emit("authenticated",encoded);
preload();
}
else socket.emit("unauthorized");
});
});
if (typeof authenticated[socket.id] !== 'object')
{
console.log(socket.id,"does not exist -- sending unauthorized");
socket.emit("unauthorized");
}
else preload();
// OTHER INDEX LISTENERS HERE
socket.on('disconnect', function(){
delete authenticated[socket.id];
console.log(socket.id,"deleted");
});
function preload()
{
console.log("preloading index");
// PRELOAD INDEX
}
});
var query = io.of('/query').on('connection', function (socket) {
console.log("client connected to QUERY with id",socket.id);
if (typeof authenticated[socket.id] !== 'object')
{
console.log(socket.id,"does not exist -- sending unauthorized");
socket.emit("unauthorized");
}
else
{
console.log("preloading query");
// PRELOAD QUERY
}
// OTHER QUERY LISTENERS HERE
socket.on('disconnect', function(){
delete authenticated[socket.id];
console.log(socket.id,"deleted");
});
});
io.use(function(socket, next){
if (socket.handshake.query.u !== 'null' && socket.handshake.query.u !== undefined)
{
authenticated[socket.id] = {};
authenticated[socket.id].user = socket.handshake.query.u;
authenticated[socket.id].group = socket.handshake.query.g;
console.log("authenticated updated ==>",authenticated);
}
if (socket.request.headers.cookie) return next();
next(new Error('Authentication error'));
});
这是我适用的客户代码
<!DOCTYPE html>
<html>
<head>
<title>Query Page</title>
<link rel="icon" href="favicon.ico" sizes="32x32" type="image/ico">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
<script>
var socket = io.connect("127.0.0.1/query",{query:"u=" + readCookie("u") + "&g=" + readCookie("g")});
我显然遗漏了一些东西。此外,我是node和socket.io的新手,非常感谢任何指导。
编辑: 将修改更改为答案。
还将客户端连接从“example.com”更改为服务器的IP地址。虽然,我怀疑这很重要,但我想知道这个问题是否与我的DNS重定向网址有关。
答案 0 :(得分:1)
根“命名空间”不会像我一样。
更改
var index = io.of('/').on('connection', function (socket) {
到
var index = io.of('/index').on('connection', function (socket) {
因此,实际索引页面不依赖于默认命名空间,而是拥有自己的模块。