Пример #1
0
        public RestResult UserLogin(string id, string name)
        {
            this.CurrentContext.Session.Tag.ID = id;
            this.CurrentContext.Session.Tag.Set("name", name);
            //发送给所有人
            var msg = name + "上线" + DateTime.Now;

            this.SendMessageToUsers(new string[] { }, msg);
            this.SendAllOnlien();
            this.SendAllTcp(msg);
            return(RestResult.True("设置成功", this.CurrentContext.Session.Tag.ID));
        }
Пример #2
0
        public RestResult SendMessageToUsers(string[] users, string message)
        {
            if (string.IsNullOrEmpty(message) == true)
            {
                return(RestResult.False("发送的内容不能为空"));
            }
            Console.WriteLine("{0} 收到后回复信息:{1}", this.CurrentContext.Session.RemoteEndPoint.ToString(), message);
            var name  = (string)this.CurrentContext.Session.Tag.Get("name").AsString(); // 发言人
            var model = new SenMessge(name, message);

            if (users.Length <= 0 || users[0] == "0")
            {
                SendAllMessage(model);
            }

            foreach (var user in users)
            {
                if (!string.IsNullOrEmpty(user.Trim()) && user != "0")
                {
                    var session = this.CurrentContext.JsonWebSocketSessions.Where(item => item.Tag.ID == user)
                                  .FirstOrDefault();
                    if (session == null)
                    {
                        model.sender  = "系统";
                        model.Content = user + " 已经离线.";
                        this.CurrentContext.Session.InvokeApi("onWebNotify", model);
                        continue;
                    }
                    //消息不发给自己
                    if (session.Tag.ID != this.CurrentContext.Session.Tag.ID)
                    {
                        session.InvokeApi("onWebNotify", model);
                    }
                }
            }
            return(RestResult.True("发送成功"));
        }