簡體   English   中英

Socket.io-Stream不發送給客戶端

[英]Socket.io-Stream not sending to Client

我正在嘗試從服務器向客戶端發送(中繼)連續的utf-8數據流。 雖然我可以關注到達服務器的數據,但我無法將其傳輸到Socket並將其轉發給客戶端。

Nodejs服務器,

var io = require('socket.io')(server);
app.io = io;
var dsteem = require('dsteem')
var es = require('event-stream') 
var client = new dsteem.Client('https://api.steemit.com')
var ss = require('socket.io-stream'); 
var outBoundStream = ss.createStream();
var stream = client.blockchain.getBlockStream();

io.on('connection', function(socket) {
    /* Eyeball the data in the nodejs console */ 
    stream.pipe(es.map(function(block, callback) {
    callback(null, util.inspect(block, {colors: true, depth: null}) + '\n')
    })).pipe(process.stdout);
    /* Send stream data to client */
    ss(socket).on('ready', function(){
        console.log('Here it comes...');
        ss(socket).emit('sending', function(){
            stream.pipe(es.map(function(block, callback) {
            callback(null, util.inspect(block, {colors: true, depth: null}) + '\n')
            }));
        });
    });

    ss(socket).on('disconnect', function(){
        console.log('Disconnected');
    });
});

我可以在nodejs控制台中看到的來自.getBlockStream()的數據看起來像這樣的剪輯,

 {['1b3c0394d1146cecdc0b5d38486f0b99a6d7c750',
 '85e3110930f87510b4782d50de86180ecbacf05d',
 '7cbd01b2a9d515736860014eabbc18893707e0c4',
 '307308574fec6336493d0a9713ee98da21bbc1a7',
 '9cb1636cdb2591361b7d7e4c9d04096c1bc93aff',
 '27d22c51a6f3e0d764039b095046ec563e53ae6b',
 '153451e251816823e3342d2f0b1425570d0f3d36',
 '0a17b03e9fddfde49ad632dfe2437d76321c34eb',
 'fdd23d78865d1855b16d171d24199dd4e2fba83a',
 '306e6f2b11e6bd6e2ef37acbe6e593ef2d1c0e1e' ] }

客戶,

<script src="/socket.io/socket.io.js"></script>
<script src="/javascripts/socket.io-stream.js"></script>
<script> 
$(function () {
    var socket = io();
    ss(socket).emit('ready', 'Client ready, send me a stream...');
    ss(socket).on('sending', function(stream, d){
            console.log(stream);
  });
});
</script>

在瀏覽器控制台中我只看到這個功能,

ƒ () {
        var args = slice.call(arguments);
        args = self.encoder.encode(args);
        ack.apply(this, args);
      }

誰能幫我理解我做錯了什么?

出現問題是因為您有以下代碼

ss(socket).emit('sending', function(){
   stream.pipe(es.map(function(block, callback) {
   callback(null, util.inspect(block, {colors: true, depth: null}) + '\n')
}))

emit方法的第二個參數需要數據,而你正在向它發送一個函數,因此你在客戶端收到的是一個空白函數

修復很簡單,只需發送數據即可

io.on('connection', function(socket) {
    /* Eyeball the data in the nodejs console */
    /* Send stream data to client */
    ss(socket).on('ready', function(){
        console.log('Here it comes...');
        stream.pipe(es.map(function(block, callback) {
            ss(socket).emit('sending', block);
            callback(null, util.inspect(block, {colors: true, depth: null}) + '\n')

        })).pipe(process.stdout);

    });

    ss(socket).on('disconnect', function(){
        console.log('Disconnected');
    });
});

完成后,您可以在客戶端查看數據

數據客戶端

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM