websockets - 从websocket服务器获取消息并将其发送到客户端

时间:2018-05-31 06:57:46

标签: javascript node.js websocket

我正在尝试学习websockets如何工作。我能够从websocket服务器获取交易数据并在控制台上打印。但是,我试图了解如何将该消息传递到我的websocket服务器,以便我可以将它发送到我的websocket客户端。基本上,我想在浏览器上打印每条消息。

"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。正如你可以看到https://github.com/websockets/ws中的一个例子,当有连接时,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服务器将数据发送到每个客户端websockets。

enter image description here

SUCCESS : 好的,我是通过在websocket服务器连接中定义消费者websocket(binanceWSmessage监听器来实现的。我不确定这是否是一个好方法

"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);
  });
});

2 个答案:

答案 0 :(得分:1)

单个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

//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

答案 1 :(得分:1)

我实现了这一点,我保留了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");
    });
});