简体   繁体   English

Websocket,边听边播

[英]Websocket, listen and broadcast at the same time

Hello I'm trying to learn node.js, I'm a old PHP coder... The reason why I'm doing that is the websockets thing... so hard with PHP and natural with node... Anyway.. I'm so new with js and node I cannot figure how to do make this work together...你好,我正在尝试学习 node.js,我是一个老 PHP 编码器......我这样做的原因是 websockets 的东西......对 PHP 来说太难了,对 node 来说很自然......无论如何......我'我对 js 和 node 太陌生了,我不知道如何使它一起工作......

I have two example, they work perfectly separately but I cannot find away to use them both at the same time...我有两个例子,它们可以完美地分开工作,但我无法同时使用它们......

It very simple I want to listen to a websocket... process the data and send it to my own websocket/server这很简单我想听 websocket... 处理数据并将其发送到我自己的 websocket/服务器

The server (work perfectly, so simple)服务器(完美运行,如此简单)

const server = new WebSocket.Server({ port: 8080 })
 
server.on('connection', serverSocket => {
  serverSocket.on('message', message => {
    console.log(`Received message => ${message}`);
  })
  serverSocket.send('Hello! Message From Server!!');
});

The client... (same here, so hard in PHP)客户端...(这里也一样,PHP 太难了)

const client = new WebSocket('wss://stream.binance.com:9443/ws');
const msg = {
  method: 'SUBSCRIBE',
  params: ['btcusdt@miniTicker'],
  id: 1,
};

client.onopen = () => {
  client.send(JSON.stringify(msg));
};

client.onmessage = e => {
      const value = e.data;
      console.log(value);
      //// process the data...
      //// and send this data to my server, like serverSocket.send(value);
};

How I can use those 2 examples in the same node file/process.我如何在同一节点文件/进程中使用这两个示例。 Again I'm so new... I did a lot of test with classes and async function and promises... with no good result most of the time I get the error我又是新手...我用类和异步 function 做了很多测试并承诺...大多数时候都没有好的结果我得到错误

node:events:505
      throw er; // Unhandled 'error' event

But if I cannot make this work in the first place no use for me to learn the rest:O)但是,如果我一开始就无法完成这项工作,那么我学习 rest:O 就没有用了)

A big thank非常感谢

Regards问候

I finally listen to Binance with a websocket, like I have in my example and I fix my issue by using socket.io to broadcast the processed data to my client.我终于用 websocket 收听 Binance,就像我在我的示例中一样,我通过使用 socket.io 将处理后的数据广播到我的客户端来解决我的问题。

This is the working example of my question.这是我的问题的工作示例。

const WebSocket = require('ws');
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

server.listen(8080, () => {
  console.log('listening on *:8080');
});

const client = new WebSocket('wss://stream.binance.com:9443/ws');
const msg = {
  method: 'SUBSCRIBE',
  params: ['troyusdt@miniTicker'],
  id: 1,
};

client.onopen = () => {
  client.send(JSON.stringify(msg));
};

client.onmessage = e => {
      const value = e.data;
      //console.log(value);
      io.emit('tokenUpdate', value); //// This is the part I was not able to do with websocket. the broadcast....
          
};

Regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM