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.
77 lines
3.2 KiB
77 lines
3.2 KiB
using Senparc.CO2NET.Trace;
|
|
using Senparc.WebSocket;
|
|
using Senparc.Weixin;
|
|
using Senparc.Weixin.WxOpen.Containers;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Znyc.Recruitment.Admin.WeChat.CommonService.MessageHandlers.WebSocket
|
|
{
|
|
/// <summary>
|
|
/// .NET Core 自定义 WebSocket 处理类
|
|
/// </summary>
|
|
public class CustomNetCoreWebSocketMessageHandler : 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;
|
|
}
|
|
|
|
string message = receivedMessage.Message;
|
|
|
|
await webSocketHandler.SendMessage("originalData:" + originalData,
|
|
webSocketHandler.WebSocket.Clients.Caller);
|
|
await webSocketHandler.SendMessage("您发送了文字:" + message, webSocketHandler.WebSocket.Clients.Caller);
|
|
await webSocketHandler.SendMessage("正在处理中(反转文字)...", webSocketHandler.WebSocket.Clients.Caller);
|
|
|
|
await Task.Delay(1000);
|
|
|
|
//处理文字
|
|
string result = string.Concat(message.Reverse());
|
|
await webSocketHandler.SendMessage(result, webSocketHandler.WebSocket.Clients.Caller);
|
|
|
|
string appId = Config.SenparcWeixinSetting.WxOpenAppId; //与微信小程序账号后台的AppId设置保持一致,区分大小写。
|
|
|
|
try
|
|
{
|
|
SessionBag sessionBag = SessionContainer.GetSession(receivedMessage.SessionId);
|
|
|
|
//临时演示使用固定openId
|
|
string openId = sessionBag != null ? sessionBag.OpenId : "onh7q0DGM1dctSDbdByIHvX4imxA"; // "用户未正确登陆";
|
|
|
|
//await webSocketHandler.SendMessage("OpenId:" + openId, webSocketHandler.WebSocket.Clients.Caller);
|
|
//await webSocketHandler.SendMessage("FormId:" + formId);
|
|
|
|
//群发
|
|
await webSocketHandler.SendMessage(
|
|
$"[群发消息] [来自 OpenId:***{openId.Substring(openId.Length - 10, 10)},昵称:{sessionBag.DecodedUserInfo?.nickName}]:{message}",
|
|
webSocketHandler.WebSocket.Clients.All);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string msg = ex.Message + "\r\n\r\n" + originalData + "\r\n\r\nAPPID:" + appId;
|
|
|
|
await webSocketHandler.SendMessage(msg,
|
|
webSocketHandler.WebSocket.Clients.Caller); //VS2017以下如果编译不通过,可以注释掉这一行
|
|
|
|
SenparcTrace.SendCustomLog("WebSocket OnMessageReceiced()过程出错", msg);
|
|
}
|
|
}
|
|
}
|
|
}
|