一段时间后,通过Python在角度Ui中进行实时数据流停止

时间:2018-08-06 14:26:44

标签: angular flask socket.io

我正在尝试从烧瓶服务器向角度UI发送连续的时间流,但是在一段时间未到达UI(即完整的180000)后,它将停止通过UI进行更新。我不明白为什么。请帮忙,谢谢。

Python代码:

from flask_socketio import SocketIO
from flask import Flask, render_template
from flask_socketio import send, emit
import time

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

breakLoop = False

@socketio.on('sendStream')
def message(arg1):
    global breakLoop
    breakLoop = True
    i=0
    maxI = 180000
    time.sleep(0.500)
    breakLoop = False
    emit('TSDetails',str(maxI))
    # data = {"count": i};
    # i += 1
    # i i<50:
    #     emit('myStream',str(data))
    #     print('message ', data)
    # else: print "limit crossed"
    while i<maxI:
        if breakLoop == True: break;
        time.sleep(1)
        i+=1000
        data = {"count":i};
        emit('myStream', str(data))

        print('message ', data)



if __name__ == '__main__':
    socketio.run(app,host= '0.0.0.0', port=5000)

角度app.componenet.ts

import { Component, OnInit, OnDestroy } from '@angular/core';
import { DataService } from './core/data.service';
import { Subscription } from 'rxjs';
import * as $ from 'jquery';
// import { Socket } from 'dgram';
import * as socketIo from 'socket.io-client';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'

})
export class AppComponent implements OnInit, OnDestroy {

  stockQuote: string;

  maxI: string;

  constructor() { }

  onButtonClick()
  {


    var socket = socketIo.connect("http://192.168.0.106:5000")
    socket.emit('sendStream',null);
    var maxI;
    socket.on('TSDetails',(res)=>{
      this.maxI = res;
      console.log(res);
    });
    socket.on('myStream', (res) => {
      // console.log("The data is: "+res);
      this.stockQuote = res;
    });

  }
  ngOnInit() {

  }

  ngOnDestroy() {
  }
}

使用stockQuote变量的代码。 app.component.html

<h1>Using Angular with WebSockets</h1>
<br />
<p>Max I is: {{maxI}}</p>
<div class="contentContainer">
    <button (click)="onButtonClick()">Start Streaming!</button> 
    {{stockQuote}}
</div>
<br />

0 个答案:

没有答案