在一個項目里,實時消息使用 asp.net core 3.0+signalr
JS 端 有一段這樣的代碼:
···
messageConnection.start().then(() => {
messageConnection.invoke('Connect', this.userid, this.name).catch(err => console.error(err.toString()))
}).catch(e => console.error(e.message))
···
當(dāng)客戶端連接成功以后,調(diào)用 服務(wù)端的connect方法,加入用戶組信息,這個方法調(diào)用時一直報錯。
報錯內(nèi)容為:Error:Failed to invoke 'Connect' due to an error on the server
另外建個方法,手寫代碼,執(zhí)行messageConnection.invoke('Connect', '1','2');這樣又不報錯。
一開始百思不得其解,后來發(fā)現(xiàn),是因為在js里面,userid為整型,在后臺的c#定義為string.原因造 成的。
修改為:
···
messageConnection.start().then(() => {
messageConnection.invoke('Connect', this.userid.toString(), this.name).catch(err => console.error(err.toString()))
}).catch(e => console.error(e.message))
···
即正常。把userid轉(zhuǎn)換為字符串即可。
這種小細節(jié),有的時候 一下子沒有反應(yīng)過來。調(diào)試起來非常郁悶。