Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        async void MessageReceived(string msg)
        {
            try
            {
                log.WriteLogToFile("IOTMsg", msg, 0);
                //  log.WriteLogToFile(msg, "WebSocketLog");

                string type = "";
                Dictionary <string, Object> dic = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(msg.ToLower());
                if (dic["content"] != null && dic["senderid"] != null)
                {
                    Dictionary <string, Object> Commands = JsonConvert.DeserializeObject <Dictionary <string, Object> >(dic["content"].ToString());

                    if (!string.IsNullOrEmpty(Commands["type"].ToString()))
                    {
                        type = Commands["type"].ToString();
                    }

                    switch (type.Trim())
                    {
                    case "dataupdate":
                        log.WriteLogToFile("云端数据更新", "WebSocketLog");
                        if (!string.IsNullOrEmpty(Commands["modulename"].ToString()))
                        {
                            Pull pull = new Pull();
                            switch (Commands["modulename"].ToString().Trim())
                            {
                            case "app":
                                await pull.PullAppData();

                                break;

                            case "dev":
                                await pull.PullDevData();

                                break;

                            case "file":
                                await pull.PullFileData();

                                break;

                            case "init":
                                await pull.PullInitData();

                                break;

                            case "prog":
                                await pull.PullProgramData();

                                break;

                            case "review":
                                await pull.PullReviewData();

                                break;

                            case "shopinfo":
                                await pull.PullShopInfoData();

                                break;

                            case "system":
                                await pull.PullSystemData();

                                break;

                            default:
                                break;
                            }
                        }
                        break;

                    case "devicecommand":
                        DbContextOptions <ContextString> options = new DbContextOptions <ContextString>();
                        ContextString dbContext = new ContextString(options);
                        log.WriteLogToFile("设备命令", "WebSocketLog");
                        if (!string.IsNullOrEmpty(Commands["devicecode"].ToString()) && !string.IsNullOrEmpty(Commands["cmdstr"].ToString()) && !string.IsNullOrEmpty(Commands["msgtype"].ToString()))
                        {
                            var         devicecode  = Commands["devicecode"].ToString();
                            var         cmdstr      = Commands["cmdstr"].ToString();
                            var         msgtype     = Commands["msgtype"].ToString();
                            MsgTemplate msgTemplate = new MsgTemplate();
                            // var dev = dbContext.Device.Where(i => i.Code == devicecode).FirstOrDefault();
                            //if (dev != null)
                            //{
                            msgTemplate.SenderID    = Method.ServerAddr;
                            msgTemplate.MessageType = msgtype;
                            msgTemplate.Content     = cmdstr;
                            msgTemplate.ReceiverID  = devicecode;
                            await Method.SendMsgAsync(msgTemplate);

                            // }
                        }
                        break;

                    default:
                        break;
                    }
                }
                if (WebSocketReceiveEvent != null)
                {
                    WebSocketReceiveEvent(type, "");
                }
            }
            catch (Exception ex)
            {
                //  log.WriteLogToFile(ex.ToString(), "WebSocketLog");
                log.WriteLogToFile("IOTMsg", ex.ToString(), 0);
            }
        }