Oki, więc takie zgłoszenie mam od nodemona
[nodemon] starting `node app.js`
Database connection
user connected 15
Gracz dolaczyl do gry - swiat:5, nacja:1, uid:15
{
'15': [
Socket {
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
nsp: [Namespace],
client: [Client],
data: {},
connected: true,
acks: Map(0) {},
fns: [],
flags: {},
server: [Server],
adapter: [Adapter],
id: '9K1j_9Y7pD9Vf3MPAAAA',
handshake: [Object],
[Symbol(kCapture)]: false
}
]
}
Kod który to generuje to:
function (err, rows) {
if (err) {
console.error(
"error connecting (sid " + req.ident + "): " + err.stack
);
return;
}
if (rows.length !== 0) {
if (rows[0].length === 0) return;
var userData = rows[0];
// join main world chat room
console.log(
"Gracz dolaczyl do gry - swiat:" +
userData.worldID +
", nacja:" +
userData.nation +
", uid:" +
userData.uid
);
socket.join("worldchat" + userData.worldID);
// join nation chat
socket.join("worldchat" + userData.worldID + "nation" + userData.nation);
// join private chat
socket.join("worldchat" + userData.worldID + "private" + userData.uid);
// join world map data
socket.join("worldmap" + userData.worldID + "nation" + userData.nation);
console.log(sockets);
A przy wysłaniu na ten adres post generuję:
chanName worldchat5
{
'15': [
Socket {
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
nsp: [Namespace],
client: [Client],
data: {},
connected: true,
acks: Map(0) {},
fns: [],
flags: {},
server: [Server],
adapter: [Adapter],
id: '9K1j_9Y7pD9Vf3MPAAAA',
handshake: [Object],
[Symbol(kCapture)]: false
}
]
}
TypeError: Cannot read property 'emit' of undefined
at C:\xampp\htdocs\project\app.js:181:36
A całe zdarzenie:
app.post("/message", jsonParser, (request, response) => {
// err is probably an invalid json error
if (request.body.password !== serverKey) {
console.log("bad password " + request.body.password);
response.statusCode = 500;
return response.end("NO U");
}
console.log('chanName',request.body.chanName);
console.log(sockets);
if (typeof request.body.chanName === "string") {
181 line: sockets[request.body.chanName].emit("message", {message: request.body.message, info: request.body.info});
} else {
for (var i in request.body.chanName) {
sockets[request.body.chanName[i]].emit("message", {message: request.body.message, info: request.body.info});
}
}
response.writeHead(200);
response.end("Dziń dybry");
});
Czyli wychodzi na to, że kanał jest zadeklarowany (worldchat5) , ale nie ma go w obiekcie sockets{} :(
Ten socket '15' to mój idik zadeklarowany tutaj:
io.on("connection", function (socket) {
if (!sockets[socket.handshake.query.user_id]) {
sockets[socket.handshake.query.user_id] = [];
}
sockets[socket.handshake.query.user_id].push(socket);