Example #1
0
        private static void AppServer_NewRequestReceived(AppSession session, SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo)
        {
            switch (requestInfo.Key.ToUpper())
            {
            case ("ECHO"):
                session.Send(requestInfo.Body);
                break;

            case ("ADD"):
                session.Send(requestInfo.Parameters.Select(p => Convert.ToInt32(p)).Sum().ToString());
                break;

            case ("MULT"):

                var result = 1;

                foreach (var factor in requestInfo.Parameters.Select(p => Convert.ToInt32(p)))
                {
                    result *= factor;
                }

                session.Send(result.ToString());
                break;
            }
        }
Example #2
0
        public static string relay(SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo, String sendstr)
        {
            bool sok = false;
            //session.Send("server noactive");
            String user = requestInfo.Body.Split(' ')[0].Split(':')[1];

            //群发广播
            if (user.IndexOf("%") > 0)
            {
                user = user.Replace("%", "");
                foreach (var us in ls)
                {
                    if (debug == true)
                    {
                        File.AppendAllText("c:\\log\\6002.log", "用户对比" + us.user + ":" + requestInfo.Body.Split(' ')[0].Split(':')[1] + "\r\n");
                    }
                    if (us.user.IndexOf(user) > -1)
                    {
                        //string[] rev = requestInfo.Body.Split(' ')[2].Split(':');
                        //for (int i=1;i<rev.Count;i++)
                        try
                        {
                            sendstr = requestInfo.Body.Split(' ')[2].Substring(4, requestInfo.Body.Split(' ')[2].Length - 4);
                            if (debug == true)
                            {
                                File.AppendAllText("c:\\log\\6002.log", "转发数据" + us.user + "数据" + sendstr + "\r\n");
                            }
                            us.sesson.Send(sendstr);
                        }
                        catch
                        {
                        };
                        sok = true;
                    }
                }
            }
            else
            {
                foreach (var us in ls)
                {
                    if (debug == true)
                    {
                        File.AppendAllText("c:\\log\\6002.log", "用户对比" + us.user + ":" + requestInfo.Body.Split(' ')[0].Split(':')[1] + "\r\n");
                    }
                    if (us.user == requestInfo.Body.Split(' ')[0].Split(':')[1])
                    {
                        //string[] rev = requestInfo.Body.Split(' ')[2].Split(':');
                        //for (int i=1;i<rev.Count;i++)
                        try
                        {
                            sendstr = requestInfo.Body.Split(' ')[2].Substring(4, requestInfo.Body.Split(' ')[2].Length - 4);
                            if (debug == true)
                            {
                                File.AppendAllText("c:\\log\\6002.log", "转发数据" + us.user + "数据" + sendstr + "\r\n");
                            }
                            us.sesson.Send(sendstr);
                        }
                        catch
                        {
                        };
                        sok = true;
                        break;
                    }
                }
            }

            if (!sok)
            {
                //requestInfo.Body.Split(' ')[2].Split(':')[1].Split(';')[0]
                foreach (var us in ls)
                {
                    if (us.user == requestInfo.Body.Split(' ')[2].Split(':')[1].Split(';')[0])
                    {
                        us.sesson.Send("server noactive");
                        break;
                    }
                }
            }
            return(sendstr);
        }
Example #3
0
        public static void server_NewRequestReceive(AppSession session, SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo)
        {
            try
            {
                if (!string.IsNullOrEmpty(requestInfo.Key) && !string.IsNullOrEmpty(requestInfo.Body))
                {
                    log.Info($"{DateTime.Now.ToString("yyyyMMdd-HHmmss:fff")}SmartHome服务器接收到新的消息key :{requestInfo.Key} Body :{requestInfo.Body}");
                }

                //MessageBox.Show(requestInfo.Key);
                //if (debug == true)
                //File.AppendAllText("c:\\log\\6002.log", requestInfo.Body);
                String sendstr = "";
                if (requestInfo != null && requestInfo.Key != null && requestInfo.Key.ToLower() == "Heartbeat".ToLower())
                {
                    //session.Send("alive");
                    return;
                }
                //GetModelype.Get()
                //JsonConvert.DeserializeObject<ServerMessage>(requestInfo.Body);
                //ChangeText c = new ChangeText(Get);
                //this.Invoke(c, "云服务器收到数据转发:"+requestInfo.Body+"\r\n");
                //if (requestInfo.Body.Split(' ')[1].Split(':')[1] == "rename")
                //{
                //    int i = 0;
                //    foreach (var us in ls)
                //    {
                //        if (us.user == requestInfo.Body.Split(' ')[0].Split(':')[1])
                //        {
                //            us.user = requestInfo.Body.Split(' ')[2].Split(':')[1];
                //            ls[i] = us;
                //            us.sesson.Send("regist sucess");
                //            return;
                //        }
                //        i++;
                //    }
                //    //ls.Add(new US() { sesson = session, user = requestInfo.Body.Split(' ')[0].Split(':')[1] });
                //    //session.Send("regist sucess");
                //    return;

                //}
                if (requestInfo.Body.Contains(";"))
                {
                    string requestType = requestInfo.Body.Split(' ')[1].Split(':')[1];
                    if (requestType == "isonline")
                    {
                        ///校验当前服务器是否有同名的用户
                        Boolean reg = true;

                        foreach (var us in ls)
                        {
                            if (debug == true)
                            {
                                File.AppendAllText("c:\\log\\6002.log", us.user + "@" + us.mac + "\r\n");
                            }
                            if ((us.user == requestInfo.Body.Split(' ')[0].Split(':')[1]) && (ToggleCase(us.mac) != ToggleCase(requestInfo.Body.Split(' ')[2].Split(':')[1])))
                            {
                                reg = false;
                                break;
                            }
                        }
                        if (reg)
                        {
                            session.Send("offline");
                        }
                        else
                        {
                            session.Send("online");
                        }
                        return;
                    }

                    if (requestType == "home")
                    {
                        ///校验当前服务器是否有同名的用户
                        Boolean reg = true;
                        foreach (var us in ls)
                        {
                            if ((us.user == requestInfo.Body.Split(' ')[0].Split(':')[1]) && (ToggleCase(us.mac) != ToggleCase(requestInfo.Body.Split(' ')[2].Split(':')[1])))
                            {
                                if (debug == true)
                                {
                                    File.AppendAllText("c:\\log\\6002.log", us.user + "@" + requestInfo.Body.Split(' ')[0].Split(':')[1] + ToggleCase(us.mac) + "@" + ToggleCase(requestInfo.Body.Split(' ')[2].Split(':')[1]) + "异地登陆被强制下线\r\n");
                                }
                                us.sesson.Send("repeat link");
                                reg = false;

                                //ls.Remove(us);
                            }
                        }

                        //if (reg)
                        //{
                        ls.Add(new US()
                        {
                            sesson = session, user = requestInfo.Body.Split(' ')[0].Split(':')[1], mac = requestInfo.Body.Split(' ')[2].Split(':')[1]
                        });
                        session.Send("regist sucess");
                        if (debug == true)
                        {
                            File.AppendAllText("c:\\log\\6002.log", "在线人数" + ls.Count + "," + DateTime.Now.ToString() + requestInfo.Body.Split(' ')[0].Split(':')[1] + "登陆服务器\r\n");
                            log.Info("在线人数" + ls.Count + "," + DateTime.Now.ToString() + requestInfo.Body.Split(' ')[0].Split(':')[1] + "登陆服务器\r\n");
                            Console.WriteLine("智能家居app端连接成功");
                        }

                        //}
                        if (!reg)
                        {
                            session.Send("repeat link");
                        }
                        return;
                    }
                    //转发消息
                    if (requestType == "other")
                    {
                        sendstr = relay(requestInfo, sendstr);

                        var u      = requestInfo.Body.Split(' ')[0].Split(':')[1];
                        var sesson = ls.FirstOrDefault(p => p.user == u);
                        //user:803802e9-bf27-4593-8ba0-96fd2e12b29d_200015010100002 type:other msg:803802e9-bf27-4593-8ba0-96fd2e12b29d_200015010100002;513;5pS+5LiA6aaW5q2M;Zip;H4sIAAAAAAAAAAEfAOD/6K6+5aSH5peg5rOV6K+G5YirQOaUvuS4gOmmluatjBG7oFsfAAAA$/r$
                        var msg = requestInfo.Body.Split(' ')[2].Substring(4, requestInfo.Body.Split(' ')[2].Length - 4);
                        if (sesson != null)
                        {
                            sesson.sesson.Send(msg);
                        }
                        else if (msg.IndexOf("513;") > 0 || msg.IndexOf("517;") > 0 || msg.IndexOf("515;") > 0)
                        {
                            //异步接收到smarthome返回结果
                            DoReceiveEvent(msg);
                        }
                        else if (msg.Contains("8212;"))
                        {
                            //注册音响
                            RegSoundHost(msg);
                        }
                        else if (msg.Contains("8215;"))
                        {
                            //是否自身播放音乐状态反转
                            UpdatePlayState(msg);
                        }
                        else if (msg.Contains("8216;"))
                        {
                            //转发给音箱,音箱自动升级的指令
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Example #4
0
        public static void server_NewRequestReceived(AppSession session, SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo)
        {
            try
            {
                //MessageBox.Show(requestInfo.Key);
                //if (debug == true)
                //File.AppendAllText("c:\\log\\6002.log", requestInfo.Body);
                String sendstr = "";
                if (requestInfo != null && requestInfo.Key != null && requestInfo.Key.ToLower() == "Heartbeat".ToLower())
                {
                    //session.Send("alive");
                    return;
                }
                //GetModelype.Get()
                //JsonConvert.DeserializeObject<ServerMessage>(requestInfo.Body);
                //ChangeText c = new ChangeText(Get);
                //this.Invoke(c, "云服务器收到数据转发:"+requestInfo.Body+"\r\n");
                //if (requestInfo.Body.Split(' ')[1].Split(':')[1] == "rename")
                //{
                //    int i = 0;
                //    foreach (var us in ls)
                //    {
                //        if (us.user == requestInfo.Body.Split(' ')[0].Split(':')[1])
                //        {
                //            us.user = requestInfo.Body.Split(' ')[2].Split(':')[1];
                //            ls[i] = us;
                //            us.sesson.Send("regist sucess");
                //            return;
                //        }
                //        i++;
                //    }
                //    //ls.Add(new US() { sesson = session, user = requestInfo.Body.Split(' ')[0].Split(':')[1] });
                //    //session.Send("regist sucess");
                //    return;

                //}

                if (requestInfo.Body.Split(' ')[1].Split(':')[1] == "isonline")
                {
                    ///校验当前服务器是否有同名的用户
                    Boolean reg = true;

                    foreach (var us in ls)
                    {
                        if (debug == true)
                        {
                            File.AppendAllText("c:\\log\\6002.log", us.user + "@" + us.mac + "\r\n");
                        }
                        if ((us.user == requestInfo.Body.Split(' ')[0].Split(':')[1]) && (ToggleCase(us.mac) != ToggleCase(requestInfo.Body.Split(' ')[2].Split(':')[1])))
                        {
                            reg = false;
                            break;
                        }
                    }
                    if (reg)
                    {
                        session.Send("offline");
                    }
                    else
                    {
                        session.Send("online");
                    }
                    return;
                }

                if (requestInfo.Body.Split(' ')[1].Split(':')[1] == "home")
                {
                    ///校验当前服务器是否有同名的用户
                    Boolean reg = true;
                    foreach (var us in ls)
                    {
                        if ((us.user == requestInfo.Body.Split(' ')[0].Split(':')[1]) && (ToggleCase(us.mac) != ToggleCase(requestInfo.Body.Split(' ')[2].Split(':')[1])))
                        {
                            if (debug == true)
                            {
                                File.AppendAllText("c:\\log\\6002.log", us.user + "@" + requestInfo.Body.Split(' ')[0].Split(':')[1] + ToggleCase(us.mac) + "@" + ToggleCase(requestInfo.Body.Split(' ')[2].Split(':')[1]) + "异地登陆被强制下线\r\n");
                            }
                            us.sesson.Send("repeat link");
                            reg = false;

                            //ls.Remove(us);
                        }
                    }

                    //if (reg)
                    //{
                    ls.Add(new US()
                    {
                        sesson = session, user = requestInfo.Body.Split(' ')[0].Split(':')[1], mac = requestInfo.Body.Split(' ')[2].Split(':')[1]
                    });
                    session.Send("regist sucess");
                    if (debug == true)
                    {
                        File.AppendAllText("c:\\log\\6002.log", "在线人数" + ls.Count + "," + DateTime.Now.ToString() + requestInfo.Body.Split(' ')[0].Split(':')[1] + "登陆服务器\r\n");
                        log.Info("在线人数" + ls.Count + "," + DateTime.Now.ToString() + requestInfo.Body.Split(' ')[0].Split(':')[1] + "登陆服务器\r\n");
                        Console.WriteLine("智能家居app端连接成功");
                    }

                    //}
                    if (!reg)
                    {
                        session.Send("repeat link");
                    }
                    return;
                }
                //转发消息
                if (requestInfo.Body.Split(' ')[1].Split(':')[1] == "other")
                {
                    sendstr = relay(requestInfo, sendstr);

                    var u      = requestInfo.Body.Split(' ')[0].Split(':')[1];
                    var sesson = ls.FirstOrDefault(p => p.user == u);
                    var msg    = requestInfo.Body.Split(' ')[2].Substring(4, requestInfo.Body.Split(' ')[2].Length - 4);
                    if (sesson != null)
                    {
                        sesson.sesson.Send(msg);
                    }
                    else
                    if (msg.IndexOf("513") > 0)
                    {
                        log.Info("智能家居nlp接收信息513 ,多线程");
                        Task.Run(() =>
                        {
                            //SmartHomeNlp.SmartHomeCallbackMsg(msg);
                            innerClassA.ReceiveMsg(msg);
                        });

                        log.Info("智能家居nlp接收信息完毕");
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Example #5
0
 /// <summary>
 /// 与Command互斥
 /// </summary>
 /// <param name="session"></param>
 /// <param name="requestInfo"></param>
 private void CustomServer_NewRequestReceived(CustomSession session, SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo)
 {
     TraceLog.WriteLine("CustomServer_NewRequestReceived...", logger: _logger);
 }