Beispiel #1
0
        private IEnumerator Start()
        {
            mHostIp = NetworkUtil.GetAddressIP();

            mSocketServer = FlexiSocket.Create(1366, Protocols.BodyLengthPrefix, false);
            mSocketServer.ClientConnected += delegate(ISocketClientToken client)
            {
                Log.i("OnClientConnected ID:{0} Count:{1}", client.ID, mSocketServer.Clients.Count);
            };
            mSocketServer.SentToClient += delegate(bool success, ISocketClientToken client)
            {
                if (success)
                {
                }
            };

            mSocketServer.ReceivedFromClient += delegate(ISocketClientToken client, byte[] message)
            {
                SocketMsg msg = SerializeHelper.FromProtoBuff <SocketMsg>(message);
                mMessage = msg.msgId + ":" + msg.ToEventID;
                if (!string.IsNullOrEmpty(msg.Msg))
                {
                    mMessage += ":" + msg.Msg;
                }
                Log.i("OnReceivedFromClient:{0}", mMessage);
                mMsgQueue.Enqueue(msg);
            };

            mSocketServer.ClientDisconnected += delegate(ISocketClientToken client)
            {
            };
            mSocketServer.StartListen(10);

            yield return(0);
        }
Beispiel #2
0
 private void AcceptCallback(object sender, SocketAsyncEventArgs args)
 {
     if (args.SocketError == SocketError.Success && args.AcceptSocket != null)
     {
         var client = new FlexiSocket(args.AcceptSocket, _protocol);
         lock (_clients)
             _clients.Add(client);
         if (ReceivedFromClient != null)
         {
             client.Received += delegate(bool success, Exception exception, SocketError error, byte[] message)
             {
                 if (success)
                 {
                     ReceivedFromClient(client, message);
                 }
             }
         }
         ;
         if (ReceivedFromClientAsString != null)
         {
             client.ReceivedAsString +=
                 delegate(bool success, Exception exception, SocketError error, string message)
             {
                 if (success)
                 {
                     ReceivedFromClientAsString(client, message);
                 }
             }
         }
         ;
         if (ClientDisconnected != null)
         {
             client.Closed += delegate
             {
                 lock (_clients)
                     _clients.Remove(client);
                 ClientDisconnected(client);
             };
         }
         if (SentToClient != null)
         {
             client.Sent +=
                 delegate(bool success, Exception exception, SocketError error)
             {
                 SentToClient(success, client);
             }
         }
         ;
         if (ClientConnected != null)
         {
             ClientConnected(client);
         }
         client.StartReceive(null, new StateObject(client._socket, _protocol));
         StartAccept(args);
     }
 }
Beispiel #3
0
        private IEnumerator Start()
        {
//			this.Sequence().Delay();

            var delayNode = new DelayNode(1.0f);

            delayNode.OnBeganCallback = () => Log.I("first began");
            delayNode.OnEndedCallback = () => Log.I("first ended");

            var delayNode2 = new DelayNode(1.0f);

            delayNode2.OnBeganCallback = () => Log.I("second began");
            delayNode2.OnEndedCallback = () => Log.I("second ended");

            yield return(SequenceNode.Allocate(delayNode, delayNode2).Execute());

            yield return(QWait.ForSeconds(1.0f));

            Log.I("from seconds");

            mHostIp = NetworkUtil.GetAddressIP();

            mSocketServer = FlexiSocket.Create(1366, Protocols.BodyLengthPrefix, false);
            mSocketServer.ClientConnected += delegate(ISocketClientToken client)
            {
                Log.I("OnClientConnected ID:{0} Count:{1}", client.ID, mSocketServer.Clients.Count);
            };
            mSocketServer.SentToClient += delegate(bool success, ISocketClientToken client)
            {
                if (success)
                {
                }
            };

            mSocketServer.ReceivedFromClient += delegate(ISocketClientToken client, byte[] message)
            {
                SocketMsg msg = SerializeHelper.FromProtoBuff <SocketMsg>(message);
                mMessage = msg.msgId + ":" + msg.ToEventID;
                if (!string.IsNullOrEmpty(msg.Msg))
                {
                    mMessage += ":" + msg.Msg;
                }
                Log.I("OnReceivedFromClient:{0}", mMessage);
                mMsgQueue.Enqueue(msg);
            };

            mSocketServer.ClientDisconnected += delegate(ISocketClientToken client) {};
            mSocketServer.StartListen(10);

            yield return(0);
        }
 /// <summary>
 /// 连接到移动设备
 /// </summary>
 /// <param name="mobileIPAddress"></param>
 /// <returns></returns>
 public void ConnectToMobile(string mobileIPAddress, ConnectedCallback onConnectionEvent)
 {
     Log.I("mobileIPAddres:{0}", mobileIPAddress);
     mSocketClient               = FlexiSocket.Create(mobileIPAddress, 1366, Protocols.BodyLengthPrefix);
     mSocketClient.Connected    += onConnectionEvent;
     mSocketClient.Disconnected += OnDisconnected;
     mSocketClient.Received     += OnReceived;
     mSocketClient.Sent         += OnSent;
     mSocketClient.Connect();
     //this.Repeat()
     //    .Delay(1.0f)
     //    .Event(() => { mSocketClient.Connect(); })
     //    .Begin();
 }
Beispiel #5
0
        /// <summary>
        /// 连接到移动设备
        /// </summary>
        /// <param name="mobileIPAddress"></param>
        /// <returns></returns>
        public void ConnectToMobile(string mobileIPAddress, ConnectedCallback onConnectionEvent)
        {
            Log.I("mobileIPAddres:{0}", mobileIPAddress);
            mSocketClient               = FlexiSocket.Create(mobileIPAddress, 1366, Protocols.BodyLengthPrefix);
            mSocketClient.Connected    += onConnectionEvent;
            mSocketClient.Disconnected += OnDisconnected;
            mSocketClient.Received     += OnReceived;
            mSocketClient.Sent         += OnSent;

            DelayNode delayNode = new DelayNode(1.0f)
            {
                OnEndedCallback = delegate
                {
                    mSocketClient.Connect();
                }
            };

            StartCoroutine(delayNode.Execute());
        }