我有以下代码
var connection = new signalR
.HubConnectionBuilder()
.withUrl("/bidHub")
.build();
connection.start().then(res => {
connection.invoke("JoinGroup", auctionId)
.catch(err => console.log(err));
}).catch (function (err) {
return console.error(err.toString());
});
ajax call here
connection.on("RefreshBids", function (currentBid, lastBidder) {
document.getElementById("currentPrice").textContent = `$${currentBid}`;
document.getElementById("lastBidder").textContent = `${lastBidder}`;
});
connection.invoke("RefreshBids", JSON.stringify(currentBid), JSON.stringify(lastBidder)).catch(function (err) {
return console.error(err.toString());
});
event.preventDefault();代码可以同时使用'connection.on‘和'connection.on’+‘connection.voke’,但是当它们都使用时,'connection.invoke‘会给出错误,它找不到'currentBid’和'lastBidder',它仍然可以工作。它似乎可以使用'connection.on',但是我是否也应该使用'connection.invoke‘呢?
发布于 2021-08-13 20:20:47
用法取决于您是否使用生成的代理。通常情况下,您将使用其中一个,但不能同时使用两个。有关更多详细信息,请参阅generated proxy文档和calling server methods文档。
https://stackoverflow.com/questions/68776144
复制相似问题