简体   繁体   English

如何在ASP.NET Core Angular应用中使用SignalR Stream?

[英]How to use SignalR Stream in ASP.NET Core Angular app?

I tried to use SignalR stream to send real-time data to client and then display the data on Angular component's template. 我尝试使用SignalR流将实时数据发送到客户端,然后在Angular组件的模板上显示数据。

Here is my code snippet, this function is bound to a button (click) event in the template: 这是我的代码段,此功能绑定到模板中的按钮(单击)事件:

    getMessage(message: string) {
    this.connection = new HubConnectionBuilder().withUrl('/messageHub').build();
    const connect = this.connection;

    connect.start().then(function () {
      connect.stream('SendMessage', message).subscribe({
        next: (item) => {
          console.log(item);   // <= this works: I can get the data from server
          let li = document.createElement("li"); // <= works
          li.textContent = item; // <= works
          document.getElementById("ulId").appendChild(li); // <= does not work, cannot get dom element by Id....
        },
        complete: () => {
          console.log("finished.");
        },
        error: (err) => {
          console.log(err);
        }
      });
    })
  }

I can't make it to work and will appreciate it if someone can provide me a working example. 我无法使其正常工作,如果有人可以为我提供工作示例,我将不胜感激。

Thanks, Jack 谢谢杰克

index.html Code be like this : index.html代码如下:

<div class="container">
    <input type="button" id="startStreaming" value="Send" />
    <ul id="discussion"></ul>
</div>

.JS code be like this : .JS代码如下:

var connection = new signalR.HubConnectionBuilder()
                .withUrl("/streaming")
                .build();
    var button = document.getElementById("startStreaming");
    function startStreaming(){
        connection.stream("StartStreaming").subscribe({
            next: onStreamReceived,
            err: function(err){
                console.log(err);
            },
            complete: function(){
                console.log("finished streaming");
            }
        });
    }
    connection.on("streamStarted", function(){
        startStreaming();
    });
    button.addEventListener("click", event => {
        connection.invoke("sendStreamInit");
    });
    function onStreamReceived(data){
        console.log("received: " + data);
        var liElement = document.createElement('li');
        liElement.innerHTML = '<strong>' + "received" + '</strong>:&nbsp;&nbsp;' + data;
        document.getElementById('discussion').appendChild(liElement);
    }
    connection.start();

You might be looking for this : 您可能正在寻找:

https://radu-matei.com/blog/signalr-core/#streaming https://radu-matei.com/blog/signalr-core/#streaming

https://github.com/radu-matei/signalr-samples/blob/master/streaming/web/wwwroot/index.html https://github.com/radu-matei/signalr-samples/blob/master/streaming/web/wwwroot/index.html

Add a property to your class messages: string[]; 在类messages: string[];添加一个属性messages: string[]; . Update your click event 更新您的点击事件

 getMessage(message: string) {
this.connection = new HubConnectionBuilder().withUrl('/messageHub').build();
const connect = this.connection;

connect.start().then(function () {
  connect.stream('SendMessage', message).subscribe({
    next: (item) => {
      this.messages.push(item);
    },
    complete: () => {
      console.log("finished.");
    },
    error: (err) => {
      console.log(err);
    }
  });
})

} }

Now set your html something like this 现在将您的html设置如下

<ul class="heroes"> <li *ngFor="let message of messages"> </li> </ul>

Now on wards for each signalR iteration your new data will be pushed into messages array and it will update in html as well. 现在在病房中,对于每个signalR迭代,您的新数据都将被推送到messages数组中,并且还将以html更新。

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

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