signalR test2

This commit is contained in:
zhougf committed 2024-03-07 23:17:12 +08:00
1 parent d250bca681
commit 99a6b6f28c
3 files changed
+57 -116

No files matched your search

@@ -31,6 +31,31 @@
datat: reactive([])
}
},
mounted() {
var _this = this;
_this.connection = new signalR.HubConnectionBuilder()
.withAutomaticReconnect()
.withUrl("/stocks")
.build();
_this.connection.start().then(function () {
_this.connection.invoke("GetAllStocks").then(function (stocks) {
for (let i = 0; i < stocks.length; i++) {
_this.datat.push({
Key: stocks[i].dbName,
Value: stocks[i].dbValue
});
}
});
_this.connection.invoke("OpenMarket");
_this.connection.invoke("GetMarketState").then(function (state) {
if (state === 'Open') {
_this.startStreaming();
}
});
});
},
methods: {
handleClick() {
var _this = this;
@@ -41,66 +66,38 @@
_this.result.forEach((item) => {
_this.datat.push(item);
})
// _this.datat.forEach((item,index) => {
// if (item.Key == "DB4.24") {
// _this.datat[index].Value = "3";
// }
// })
})
.catch(function (error) {
console.log(error);
});
},
handleSubmit(data) {
console.log(data);
this.$message.info('This is an info message')
},
startStreaming() {
this.connection.stream("StreamStocks").subscribe({
close: false,
next: this.displayStock,
error: function (err) {
logger.log(err);
}
});
},
displayStock(stock) {
this.datat.forEach((item, index) => {
if (item.Key == stock.dbName) {
this.datat[index].Value = stock.dbValue;
}
})
}
}
});
app
.use(ArcoVue)
app.use(ArcoVue)
.use(ArcoVueIcon)
.mount('#app');
console.log(app._instance.data);
let connection = new signalR.HubConnectionBuilder()
.withAutomaticReconnect()
.withUrl("/stocks")
.build();
connection.start().then(function () {
connection.invoke("GetAllStocks").then(function (stocks) {
for (let i = 0; i < stocks.length; i++) {
app._instance.data.datat.push({ Key: stocks[i].dbName, Value: stocks[i].dbValue });
}
});
connection.invoke("GetMarketState").then(function (state) {
console.log(state);
// if (state === 'Open') {
startStreaming();
// }
});
connection.invoke("OpenMarket");
});
function startStreaming() {
connection.stream("StreamStocks").subscribe({
close: false,
next: displayStock,
error: function (err) {
logger.log(err);
}
});
}
function displayStock(stock) {
app._instance.data.datat.forEach((item,index) => {
if (item.Key == stock.dbName) {
app._instance.data.datat[index].Value = stock.dbValue;
}
})
}
</script>