Javascript websocket—从websocket服务器获取消息并将其发送到客户端
我正在努力学习WebSocket是如何工作的。我能够从websocket服务器获取交易数据并在控制台上打印。然而,我试图理解如何将该消息传递到我的websocket服务器,以便将其发送到我的websocket客户端。基本上,我想在浏览器上打印每条消息Javascript websocket—从websocket服务器获取消息并将其发送到客户端,javascript,node.js,websocket,Javascript,Node.js,Websocket,我正在努力学习WebSocket是如何工作的。我能够从websocket服务器获取交易数据并在控制台上打印。然而,我试图理解如何将该消息传递到我的websocket服务器,以便将其发送到我的websocket客户端。基本上,我想在浏览器上打印每条消息 "use strict"; const WebSocket = require("ws"); const binanceWS = new WebSocket("wss://stream.binance.com:9443/ws/stratbtc@
"use strict";
const WebSocket = require("ws");
const binanceWS = new WebSocket("wss://stream.binance.com:9443/ws/stratbtc@trade");
binanceWS.on("open", function open() {
console.log("open action");
});
binanceWS.on("message", function incoming(data) {
console.log(data);
});
现在,这个binanceWS
将在收到数据时打印数据。我试图做的事情是如何传递到我的WebSocket.Server对象的send
eventlistener。如您所见,wss本身的一个示例在存在连接时使用websocket对象
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
谢谢
注意:我所说的结构是这样的。基本上,我会让websocket消费者获取交易数据。在同一个主机(目前为localhost)中,将有一个websocket服务器。此websocket服务器将向每个客户端websocket发送数据
成功:
好的,我是通过在websocket服务器连接中定义消费者websocket(binanceWS
)message
侦听器来实现的。但我不确定这是不是一个好办法
"use strict";
const WebSocket = require("ws");
const binanceWS = new WebSocket("wss://stream.binance.com:9443/ws/stratbtc@trade");
binanceWS.on("open", function open() {
console.log("open action");
});
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
binanceWS.on("message", function incoming(data) {
ws.send(data);
});
});
单个网匣
//websocket connection event will return a socket you can later use
binanceWS.on("connection", function(socket) {
socket.on("message", function incoming(data) {
console.log(data);
// if you want to send that message back to the client who sent it,
// you can use send method on the socket
socket.send(data)
});
});
//websocket connection event will return a socket you can later use
binanceWS.on("connection", function(socket) {
// create another websocket here
const wss = new WebSocket.Server({ port: 8080 });
// connect the second websocket
wss.on("connection", function(ws) {
// start listening to first websocket incoming messages
// if second communication has successully been instantiated
socket.on("message", function incoming(data) {
// send the data using the second websocket
ws.send(data)
})
});
});
const socket = new WebSocket('server_url'); // Connection opened
socket.addEventListener('message', function (event) { socket.send('send message'); });
嵌套websocket
//websocket connection event will return a socket you can later use
binanceWS.on("connection", function(socket) {
socket.on("message", function incoming(data) {
console.log(data);
// if you want to send that message back to the client who sent it,
// you can use send method on the socket
socket.send(data)
});
});
//websocket connection event will return a socket you can later use
binanceWS.on("connection", function(socket) {
// create another websocket here
const wss = new WebSocket.Server({ port: 8080 });
// connect the second websocket
wss.on("connection", function(ws) {
// start listening to first websocket incoming messages
// if second communication has successully been instantiated
socket.on("message", function incoming(data) {
// send the data using the second websocket
ws.send(data)
})
});
});
const socket = new WebSocket('server_url'); // Connection opened
socket.addEventListener('message', function (event) { socket.send('send message'); });
然后,您需要在您的客户机中有这个最小的代码,您将看到服务器和客户机之间的交互
客户端
//websocket connection event will return a socket you can later use
binanceWS.on("connection", function(socket) {
socket.on("message", function incoming(data) {
console.log(data);
// if you want to send that message back to the client who sent it,
// you can use send method on the socket
socket.send(data)
});
});
//websocket connection event will return a socket you can later use
binanceWS.on("connection", function(socket) {
// create another websocket here
const wss = new WebSocket.Server({ port: 8080 });
// connect the second websocket
wss.on("connection", function(ws) {
// start listening to first websocket incoming messages
// if second communication has successully been instantiated
socket.on("message", function incoming(data) {
// send the data using the second websocket
ws.send(data)
})
});
});
const socket = new WebSocket('server_url'); // Connection opened
socket.addEventListener('message', function (event) { socket.send('send message'); });
注意:由于您将使用一个websocket的传入数据进入另一个websocket,因此最好仅在第一个websocket已成功打开时打开第二个websocket我通过为websocket客户端保留全局列表实现了这一点。当消费者websocket消息事件被触发时,它通过遍历列表发送每个客户端
"use strict";
const WebSocket = require("ws");
const binanceWS = new WebSocket("wss://stream.binance.com:9443/ws/eosbtc@trade");
var websocketList = [];
binanceWS.on("open", function open() {
console.log("open action");
});
binanceWS.on("message", function incoming(data) {
console.log(data);
// send data to every websocket client
websocketList.forEach(ws => {
ws.send(data);
});
});
const wss = new WebSocket.Server({ port: 8080 });
wss.on("connection", function connection(ws) {
// add ws handle to websocket list.
websocketList.push(ws);
ws.on("close", function close() {
console.log("Disconnected");
});
});
如果我错了,请纠正我。这里您需要一个客户端websocket连接到
binanceWS
。建立连接后,客户端websocket可以向binanceWS发送消息,binanceWS将发送回该消息?是的,就是这样!谢谢你的回复,但我有点困惑。我试图在触发binanceWS.on('message')
时发送数据。为此,我需要创建一个新的WebSocket.Server,并使用该WebSocket服务器对象发送消息。实际上,当触发message
事件时,我的代码段会将数据发送回客户端。这意味着在您的客户机上,您应该有如下内容:constsocket=newwebsocket('server_url');socket.addEventListener('message',函数(事件){socket.send('send message');})代码>我将更新我的答案Hey Edkeveke我用一个小结构更新了我的帖子,我正在尝试完成这个小结构。基于我的结构binanceWS
一个从exchange获取数据的消费者websocket。然后我需要创建一个websocket服务器,它将从binanceWS获取的数据发送到客户端websocket。清楚了吗?谢谢你能帮我把你的客户发过来吗?我想了解如何在客户端检索websocketList中的项?有点晚,但。。。您不需要保留客户机列表,根据文档()的规定,您应该在wss中拥有连接的客户机列表,您可以使用该列表进行循环。