예제 #1
0
        /// <summary>
        /// 收到来自client的消息
        /// </summary>
        /// <param name="socketAsyncEventarfs"></param>
        /// <param name="token"></param>
        /// <param name="buff"></param>
        static void m_socket_ReceiveClientData(AsyncUserToken token, byte[] buff)
        {
            // Console.WriteLine("收到client来的数据");
            string msg = Encoding.UTF8.GetString(buff);

            //   Console.WriteLine("Rece:{0}", msg);
            Log.Info(msg);
            ///发送消息
            byte[] message = Encoding.UTF8.GetBytes("success:" + msg);
            bool   issent  = serverHandler.SendMessage(token, message);

            if (issent)
            {
                Log.Info("success:" + msg);
            }

            //转发到另外一个client上,如果收到心跳包heartbeat,则不转发,直接serverhui
            if (msg == "heartbeat")
            {
                return;
            }

            //将下面代码封装一下。
            if (serverHandler.ClientList.Count > 1)
            {
                AsyncUserToken ntoken = serverHandler.ClientList.Find(s => s.IPAddress != token.IPAddress);
                if (ntoken != null)
                {
                    serverHandler.SendMessage(ntoken, buff);
                }
            }
        }