public void Write(System.Net.Sockets.Socket sender, IList <System.Net.Sockets.Socket> cliens, IDictionary <string, object> param)
        {
            Console.WriteLine("客户端({0})发送消息", sender.Handle);
            var msgList = param["Message"] as IEnumerable <object>;

            if (msgList == null)
            {
                return;
            }

            var response = new SocketResponse
            {
                Method   = "Send",
                DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                Result   = new
                {
                    UserName = param["UserName"].ToString(),
                    Message  = msgList.Select(s => s.ToString()).ToArray()
                }
            };

            JavaScriptSerializer jss = new JavaScriptSerializer();
            string context           = jss.Serialize(response);

            Console.WriteLine("消息发送的数据为:{0}", context);

            Parallel.ForEach(cliens, (item) =>
            {
                try
                {
                    item.Send(Encoding.UTF8.GetBytes(context + "\n"));
                }
                catch { };
            });
        }
Ejemplo n.º 2
0
        public void Write(System.Net.Sockets.Socket sender, IList <System.Net.Sockets.Socket> cliens, IDictionary <string, object> param)
        {
            Console.WriteLine("客户端({0})登陆", sender.Handle);
            var response = new SocketResponse
            {
                Method   = "Logon",
                DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                Result   = new { UserName = param["UserName"].ToString() }
            };

            JavaScriptSerializer jss = new JavaScriptSerializer();
            string context           = jss.Serialize(response);

            Console.WriteLine("登陆发送的数据为:{0}", context);
            sender.Send(Encoding.UTF8.GetBytes(context + "\n"));
        }