You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.8 KiB

2 years ago
/* senparc.websocket.js - 20191001 - v2.0 */
/* 支持 .NET Core 3.0+ SignalR。 */
define('js/senparc.websocket.2.0', function () {
var senparcWebSocketConnection;
function buildConnectionAndStart(hubUrl, signalR, onStart) {
senparcWebSocketConnection = new signalR.HubConnectionBuilder()
.withUrl(hubUrl)
//.withAutomaticReconnect([0, 2000, 5000, 10000, 30000, 45000, 60000])
.build();
senparcWebSocketConnection.start(onStart()).then().catch(function (err) {
return console.error(err.toString());
});
return senparcWebSocketConnection;
}
function sendMessage(text, sessionId, formId) {
//如果使用 Senparc.WebSocket必须严格按照以下 submitData 数据字段发送(参数只能多不能少)
var submitData = JSON.stringify({
Message: text,//必填
SessionId: sessionId,//选填,不需要可输入''
FormId: formId//选填formId用于发送模板消息不需要可输入''
});
//ReceiveMessage 为特殊约定的方法入口,请勿修改,如果使用其他名称,则会对应到 SenparcHub 下的其他自定义方法
senparcWebSocketConnection.invoke("ReceiveMessage", submitData).catch(function (err) {
return console.error(err.toString());
});
}
function onReceiveMessage(receive) {
senparcWebSocketConnection.on("ReceiveMessage", function (res) {
receive(res);
});
}
//module.exports = {
// buildConnectionAndStart: buildConnectionAndStart,
// sendMessage: sendMessage,
// onReceiveMessage: onReceiveMessage
//}
return {
buildConnectionAndStart: buildConnectionAndStart,
sendMessage: sendMessage,
onReceiveMessage: onReceiveMessage
};
});