有没有办法使用 JS 客户端 SignalR 库发送 JSON 正文?
我正在尝试找到一种使用以下 JS 代码添加 JSON 正文的方法
const connection = new signalR.HubConnectionBuilder().withUrl("https://[host]/[path]",
{headers:{"Authorization": "Bearer [token]","Content-Type": "application/json","Accept": "application/json"
},transport: signalR.HttpTransportType.LongPolling})
.configureLogging(signalR.LogLevel.Trace)
.withAutomaticReconnect()
.build()
亲切的问候
找不到提供请求正文的选项。
谢谢你的建议,我尝试了一下,成功了。
const InitializeSignalRConnection = () => {
const headers = {
'Authorization': 'Bearer [jwt_token]',
'Content-Type': 'application/json',
'Accept': 'application/json, application/problem+json; charset=utf-8'
}
const negotiateReq = {
'k1': 'v1',
'k2': 'v2'
}
axios.post("https://[host][path]/negotiate?negotiateVersion=1", negotiateReq, {
headers: headers
})
.then((response) => {
const connection = new signalR.HubConnectionBuilder()
.withUrl(response.data.url,
{
accessTokenFactory: () => response.data.accessToken,
transport: signalR.HttpTransportType.LongPolling
}
)
.configureLogging(signalR.LogLevel.Trace)
.withAutomaticReconnect()
.build()
connection.on('Notified', receiveNotifications)
connection.start()
.catch(err => console.error(err.toString()));
return connection;
})
}