给定一个诸如:'ws‘这样的URL,我想发送两种类型的消息:
我知道很多交易所都是这样做的,有人能给我一个线索吗?
今天我发现了一个非常有趣的网站,它通过一个看起来像公共频道的websocket发送个人信息。
我的问题是:如何做到这一点?websocket服务器可以通过某种秘密方式发送私密信息吗?

以下是websocket频道的详细信息:
一般情况:
Request URL: wss://wbs.mxc.com/socket.io/?EIO=3&transport=websocket
Request Method: GET
Status Code: 101 Switching Protocols响应头:
HTTP/1.1 101 Switching Protocols
Date: Tue, 03 Sep 2019 06:13:55 GMT
Connection: upgrade
Server: nginx
upgrade: websocket
sec-websocket-accept: xr3/mMY887Utp3cnZdf37ycDWAc=
sec-websocket-extensions: permessage-deflate请求头:
GET wss://wbs.mxc.com/socket.io/?EIO=3&transport=websocket HTTP/1.1
Host: wbs.mxc.com
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: https://www.mxc.com
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7
Sec-WebSocket-Key: oYmhqikSoGD8AgdqrMj0XQ==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits这是一个关于密码交换的网站。
客户端用户可以接收公共信息(密码价格...etc),登录用户可以通过相同的URL接收他的帐户余额。
那怎么做呢?
我也在其他网站上看到过这样的东西,比如huobi.pro,(EIO=3...),这是一种websocket客户端框架吗?
非常感谢
发布于 2019-09-07 02:13:08
首先,重要的是要理解WebSocket连接不是一个愚蠢的广播。
恰恰相反:从技术上讲,每个连接客户端从一开始就获得到服务器的(私有) WebSocket连接,而服务器端则使用唯一的连接id引用服务器端。
记住这一点,其余的都很容易解释:服务器现在可以使用通常的方法来进一步识别客户端。在这样一个市场上,这通常只会发生在登录。之后,服务器知道具有唯一ID的WebSocket连接属于您的特定用户帐户。从现在开始,它将开始不仅将“公共”信息推送到您的WebSocket连接,而且还会将仅用于您帐户的私有信息推送到您的帐户中。
有这样的框架,但是只发送两种类型的消息,一种是针对所有用户,另一种是针对特定用户,您可能不需要一种,或者至少不是非常复杂的一种。
然而,实际上非常流行的是socket.io,这也是一个很好的匹配。socket.io为您提供了一个方便的API来使用WebSockets,并支持私有消息和智能广播。为了说明这一点,请考虑以下伪代码:
const io = require('socket.io')();
io.on('connection', (socket) => {
// Already at this point you can reference the unique connected client by socket.id
// However, currently we are not interested in the unique client
// and just want to send 'public' information to everybody:
socket.emit('Pubic message everybody will get');
socket.on('login', loginData => {
// Obviously you would authenticate against a database and a lot more sophisticated ;-)
if (loginData.name === "Rose" && loginData.pass === "superStrongPassword") {
socket.emit('Hello Rose, this is your private message');
// And since you are now logged in maybe also check for something in any kind
// of DB collection and lets inform you whenever there is an update
OrdersCollection.find({'owner':'Rose'}).observe({
changed: updatedOrder => {
socket.emit('One of your orders you placed with us was just updated. Here is the current order:', updatedOrder);
}
})
}
});
});https://stackoverflow.com/questions/57766181
复制相似问题