//DPBMARK_FILE WebSocket
#if NET462
using System;
using System.Linq;
using System.Threading.Tasks;
using Senparc.WebSocket;
using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;
using Senparc.Weixin.WxOpen.Containers;
using System.Web.Configuration;
using Senparc.Weixin.Sample.CommonService.TemplateMessage.WxOpen;
namespace Senparc.Weixin.Sample.CommonService.MessageHandlers.WebSocket
{
///
/// 自定义 WebSocket 处理类
///
public class CustomNet45WebSocketMessageHandler : WebSocketMessageHandler
{
public override Task OnConnecting(WebSocketHelper webSocketHandler)
{
//TODO:处理连接时的逻辑
return base.OnConnecting(webSocketHandler);
}
public override Task OnDisConnected(WebSocketHelper webSocketHandler)
{
//TODO:处理断开连接时的逻辑
return base.OnDisConnected(webSocketHandler);
}
public override async Task OnMessageReceiced(WebSocketHelper webSocketHandler, ReceivedMessage receivedMessage, string originalData)
{
if (receivedMessage == null || string.IsNullOrEmpty(receivedMessage.Message))
{
return;
}
var message = receivedMessage.Message;
await webSocketHandler.SendMessage("originalData:" + originalData);
await webSocketHandler.SendMessage("您发送了文字:" + message);
await webSocketHandler.SendMessage("正在处理中...");
await Task.Delay(1000);
//处理文字
var result = string.Concat(message.Reverse());
await webSocketHandler.SendMessage(result);
#if NET462
var appId = Config.SenparcWeixinSetting.WxOpenAppId;//与微信小程序账号后台的AppId设置保持一致,区分大小写。
#else
var appId = "WxOpenAppId";//与微信小程序账号后台的AppId设置保持一致,区分大小写。
#endif
try
{
//发送模板消息
var formId = receivedMessage.FormId;//发送模板消息使用,需要在wxml中设置