/// <summary> /// 发送websocket消息 /// </summary> /// <returns></returns> public static async Task <bool> SendMsgAsync(MsgTemplate msg) { bool _Result = false; try { if (!ChatWebSocketMiddleware._sockets.ContainsKey(msg.ReceiverID)) { //不存在接收端连接 return(_Result); } var socket = ChatWebSocketMiddleware._sockets.Where(i => i.Key == msg.ReceiverID).FirstOrDefault(); if (socket.Value.State != WebSocketState.Open) { //接收端已断开 return(_Result); } // 控制只有接收者才能收到消息 await ChatWebSocketMiddleware.SendStringAsync(socket.Value, JsonConvert.SerializeObject(msg), CancellationToken.None); _Result = true; } catch { throw; } return(_Result); }