小编ghe*_*rdi的帖子

在 Angular 中连接 WebSocket

我在 Node 中有 WebSocket 连接,工作正常。现在我想在 Angular 中使用它和 rxjs websocket,但我不知道如何解决连接问题。

const WebSocket = require('ws');

const ws = new WebSocket('wss://example.com');


ws.on('open', function open() {
  // on connection, send our authentication request
  ws.send(JSON.stringify({action: 'auth', key: apiKey, secret: apiSecret}));  
});

ws.on('close', function close() {
  console.log('disconnected');
});

ws.on('message', function incoming(data) {
  console.log(data)
  var msg = JSON.parse(data);
  if (msg.type === 'status' && msg.status === 'authenticated') {
    // if we got authentication confirmation, send subscribe event to the server
    ws.send(JSON.stringify({action: 'subscribe', buckets: ['exampleBucket']}));
  }
});
Run Code Online (Sandbox Code Playgroud)

websocket rxjs angular

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

angular ×1

rxjs ×1

websocket ×1