如何使用websocket在具有相同令牌的用户之间发送消息



我使用webSocket从HTML文件发送消息。

const socket = new WebSocket("ws://localhost:3000/{{ token }}");
socket.send(JSON.stringify(message));

token将来自浏览器URL。

server.js

const WebSocket = require('ws');
const qs = require('querystring');
const http = require('http');
const wss = new WebSocket.Server({ port: 3000 });
const webSockets = {};

wss.on('connection', function (ws, req)
{
let url = req.url;
let userToken = url.substring(url.lastIndexOf('/') + 1);
webSockets[userToken] = ws;

ws.on('message', function incoming(message)
{
let messageArray = JSON.parse(message);
let token = messageArray['token'];
let toUserWebSocket = webSockets[token];
toUserWebSocket.send(message);

我已经打开了3个浏览器与以下URL

`https://url/same_token`
`https://url/same_token`
`https://url/different_token`

所有浏览器都在接收消息。如果消息是从send_token发送的,我希望消息只能由具有same_token的浏览器接收

我无法按照描述重现问题,但只能接收到发送给具有相同token的连接客户端中的一个客户端的消息。

单个连接客户端的问题是由于引用了webSockets[userToken] = ws,而不是使用客户端阵列webSockets[userToken] = [];webSockets[userToken].push(ws);

还要确保没有node server.js的僵尸进程在后台运行。

//...
const clients = {};
wss.on('connection', function connection(ws, req) {
let id = req.url.substring(req.url.lastIndexOf('/') + 1);
if ('' === id) {
//connecting id is missing
ws.terminate();
return;
}
if ('undefined' === typeof clients[id]) {
clients[id] = [];
}
console.log('Connection Received from IP: ' + req.socket.remoteAddress + ' with id ' + id);
//append websocket client to list of clients
clients[id].push(ws);
ws.on('message', function incoming(message) {
if ('' === message) {
//skip if message is blank
return;
}
try {
//process message as JSON object as "{"message": "string", "token": "string"}"
message = JSON.parse(message);
} catch(e) {
return;
}
if (!message.hasOwnProperty('token') || '' === message.token) {
//token was not sent or is empty
return;
}
let token = message.token;
console.log('Message received for token ' + token);
if ('undefined' !== typeof clients[token] && clients[token]) {
clients[token].forEach(function(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
//do not send message back to the sending client
client.send(message.message + ' ' + token);
}
});
}
//inform sending client message was sent
ws.send('Sent: ' + message.message);
});
ws.on('close', function() {
clients[id].forEach(function(client, index) {
if (client === ws) {
//remove the client from the pool
clients[id].splice(index, 1);
}
});
});
});

html代码

<button type="button" id="sendBtn" class="btn-primary">Send</button>
<script type="text/javascript">
const socket = new WebSocket('ws://localhost:8080/{{ token }}');
socket.onmessage = function(evt) {
window.console.log(evt.data);
};
jQuery(function($) {
$('#sendBtn').on('click', function() {
const message = {
"message": "Hello World",
"token": "{{ token }}"
};
socket.send(JSON.stringify(message));
});
});
</script>

最新更新