admin管理员组文章数量:1279112
I am trying to learn how websockets work. I am able to get trading data from a websocket server and print it on the console. However, I am trying to understand how I can pass that message into my websocket server so that I can send it to my websocket clients. Basically, I want to print each message on browser.
"use strict";
const WebSocket = require("ws");
const binanceWS = new WebSocket("wss://stream.binance:9443/ws/stratbtc@trade");
binanceWS.on("open", function open() {
console.log("open action");
});
binanceWS.on("message", function ining(data) {
console.log(data);
});
Now this binanceWS
will print data when it recieves one. The thing I am trying to do is how I can pass to the send
eventlistener of my WebSocket.Server object. As you can see an example from wss itself takes a websocket object when there is a connection.
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function ining(message) {
console.log('received: %s', message);
});
ws.send('something');
});
Thanks!
NOTE: The structure I am talking about is like this. Basically, I will have websocket consumers to get trade data. And within same host (localhost for now) there will be a websocket server. This websocket server will send data to each client websockets.
SUCCESS:
Ok I made it by defining consumer websocket (binanceWS
) message
listener in websocket server connection. I am not sure if this is a good way though
"use strict";
const WebSocket = require("ws");
const binanceWS = new WebSocket("wss://stream.binance: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 ining(message) {
console.log('received: %s', message);
});
binanceWS.on("message", function ining(data) {
ws.send(data);
});
});
I am trying to learn how websockets work. I am able to get trading data from a websocket server and print it on the console. However, I am trying to understand how I can pass that message into my websocket server so that I can send it to my websocket clients. Basically, I want to print each message on browser.
"use strict";
const WebSocket = require("ws");
const binanceWS = new WebSocket("wss://stream.binance.:9443/ws/stratbtc@trade");
binanceWS.on("open", function open() {
console.log("open action");
});
binanceWS.on("message", function ining(data) {
console.log(data);
});
Now this binanceWS
will print data when it recieves one. The thing I am trying to do is how I can pass to the send
eventlistener of my WebSocket.Server object. As you can see an example from https://github./websockets/ws wss itself takes a websocket object when there is a connection.
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function ining(message) {
console.log('received: %s', message);
});
ws.send('something');
});
Thanks!
NOTE: The structure I am talking about is like this. Basically, I will have websocket consumers to get trade data. And within same host (localhost for now) there will be a websocket server. This websocket server will send data to each client websockets.
SUCCESS:
Ok I made it by defining consumer websocket (binanceWS
) message
listener in websocket server connection. I am not sure if this is a good way though
"use strict";
const WebSocket = require("ws");
const binanceWS = new WebSocket("wss://stream.binance.: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 ining(message) {
console.log('received: %s', message);
});
binanceWS.on("message", function ining(data) {
ws.send(data);
});
});
Share
Improve this question
edited May 31, 2018 at 8:18
quartaela
asked May 31, 2018 at 6:57
quartaelaquartaela
2,75720 gold badges70 silver badges109 bronze badges
2 Answers
Reset to default 3I achieved this my keeping a global list for websocket clients. And when consumer websocket message event is triggered, it sends every client by traversing list.
"use strict";
const WebSocket = require("ws");
const binanceWS = new WebSocket("wss://stream.binance.:9443/ws/eosbtc@trade");
var websocketList = [];
binanceWS.on("open", function open() {
console.log("open action");
});
binanceWS.on("message", function ining(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");
});
});
single websocket
//websocket connection event will return a socket you can later use
binanceWS.on("connection", function(socket) {
socket.on("message", function ining(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)
});
});
Nesting websocket
//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 ining messages
// if second munication has successully been instantiated
socket.on("message", function ining(data) {
// send the data using the second websocket
ws.send(data)
})
});
});
Then you need to have this minimum code in your client and you will see the interaction between the server and your client
client
const socket = new WebSocket('server_url'); // Connection opened
socket.addEventListener('message', function (event) { socket.send('send message'); });
Note: Since you will use the ining data of a websocket into another, it is best to open the second websocket only if the first one has successfully been opened
本文标签: javascriptwebsocketsget message from a websocket server and send it to clientStack Overflow
版权声明:本文标题:javascript - websockets - get message from a websocket server and send it to client - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741282675a2370097.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论