Exemplo n.º 1
0
 public object sendmsg([FromForm] Guid senderWebsocketId, [FromForm] Guid receiveWebsocketId, [FromForm] string message, [FromForm] bool isReceipt = false)
 {
     WebChatHelper.SendMsg(senderWebsocketId, new[] { receiveWebsocketId }, message, isReceipt);
     return(new {
         code = 0
     });
 }
Exemplo n.º 2
0
        public static void RedisSubsrcMessage(CSRedis.CSRedisClient.SubscribeMessageEventArgs e)
        {
            //注意:redis服务重启以后,需要重新启动服务,不然redis通道无法重连
            try {
                var msg = WebChatMessage.Parse(e.Body);
                switch (msg.Type)
                {
                case WebChatMessageType.发送消息:
                    var data = msg.GetData <(string sender, string[] receives, string content, bool receipt)>();
                    Console.WriteLine($"收到消息:{data.content}" + (data.receipt ? "【需回执】" : ""));

                    var outgoing = new ArraySegment <byte>(Encoding.UTF8.GetBytes(data.content));
                    foreach (var uid in data.receives)
                    {
                        List <WebSocketHandler> sock = null;
                        if (_websockets.TryGetValue(uid, out sock) == false)
                        {
                            //Console.WriteLine($"websocket{uid} 离线了,{data.content}" + (data.receipt ? "【需回执】" : ""));
                            if (uid != data.sender && data.receipt)
                            {
                                WebChatHelper.SendMsg(Guid.Parse(uid), new Guid[] { Guid.Parse(data.sender) }, new {
                                    data.content,
                                    receipt = "用户不在线"
                                });
                            }
                            //未找到socket
                            continue;
                        }

                        WebSocketHandler[] sockarray;
                        try {
                            sockarray = sock.ToArray();
                        } catch {
                            lock (_websockets_lock)
                                sockarray = sock.ToArray();
                        }

                        //如果接收消息人是发送者,并且接口端只有1个以下,则不发送
                        //只有接口者为多端时,才转发消息通知其他端
                        if (uid == data.sender && sockarray.Length <= 1)
                        {
                            continue;
                        }

                        foreach (WebSocketHandler sh in sockarray)
                        {
                            sh.socket.SendAsync(outgoing, WebSocketMessageType.Text, true, CancellationToken.None);
                        }

                        if (uid != data.sender && data.receipt)
                        {
                            WebChatHelper.SendMsg(Guid.Parse(uid), new Guid[] { Guid.Parse(data.sender) }, new {
                                data.content,
                                receipt = "发送成功"
                            });
                        }
                    }
                    break;

                case WebChatMessageType.线:
                    lock (_websockets_lock)
                        _websockets.Remove(string.Concat(msg.Data));
                    break;

                case WebChatMessageType.线:
                    break;
                }
            } catch (Exception ex) {
                Console.WriteLine($"订阅方法出错了:{ex.Message}");
            }
        }