Answer the question
In order to leave comments, you need to log in
How to send a websocket to a specific user?
Given:
Server side:
server.get('/updateData', (req, res, next) => {
if (!res.claimUpgrade) {
next(new Error('Connection Must Upgrade For WebSockets'));
return;
}
const upgrade = res.claimUpgrade();
const shed = ws.accept(req, upgrade.socket, upgrade.head);
shed.on('text', function (msg) {
acceptedUsername = msg;
console.log('User: ' + msg);
const response = {
username: acceptedUsername,
msg: "Update"
};
shed.send(JSON.stringify(response));
});
next(false);
});
authForSync.onopen = function (event) {
console.log("Opened socket!");
authForSync.send(user.username);
};
authForSync.onmessage = function (event) {
if ((JSON.parse(event.data)).username === user.username) {
console.log((JSON.parse(event.data)).username);
}
syncWithServer.close();
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question