Exemplo n.º 1
0
        private static void Listen(object state)
        {
            var context = (CampfireRoomListenerContext)state;

            try
            {
                var request  = context.CreateRequest();
                var response = request.GetResponse();
                Robot.Log.WriteInfo(string.Format("Listening to Room {0}", context.RoomId));

                using (Stream stream = response.GetResponseStream())
                {
                    if (stream != null)
                    {
                        while (stream.CanRead && context.IsListening)
                        {
                            int value = stream.ReadByte();

                            if (value == -1)
                            {
                                break;
                            }
                            if (value == 32)
                            {
                                continue;
                            }

                            // message is hear time to read.....
                            string message = ((char)value).ToString();

                            do
                            {
                                value = stream.ReadByte();

                                if (value == -1)
                                {
                                    break;
                                }
                                message += (char)value;
                            } while (value != 13);

                            var messageRecievedContext = new CampfireMessageRecievedContext(message, context.MessageRecieved);

                            if (messageRecievedContext.Message.UserId != context.UserId.ToString(CultureInfo.InvariantCulture))
                            {
                                ThreadPool.QueueUserWorkItem(ProcessMessage, messageRecievedContext);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Robot.Log.WriteError(string.Format("An error occured when listening in room {0}", context.RoomId), ex);
                Thread.Sleep(1000);
                Listen(context);
            }
        }
Exemplo n.º 2
0
        private static void Listen(object state)
        {
            var context = (CampfireRoomListenerContext)state;

            try
            {
                var request = context.CreateRequest();
                var response = request.GetResponse();
                Robot.Log.WriteInfo(string.Format("Listening to Room {0}", context.RoomId));

                using (Stream stream = response.GetResponseStream())
                {
                    if (stream != null)
                    {
                        while (stream.CanRead && context.IsListening)
                        {
                            int value = stream.ReadByte();

                            if (value == -1)
                                break;
                            if (value == 32)
                                continue;

                            // message is hear time to read.....
                            string message = ((char)value).ToString();

                            do
                            {
                                value = stream.ReadByte();

                                if (value == -1)
                                    break;
                                message += (char)value;
                            } while (value != 13);

                            var messageRecievedContext = new CampfireMessageRecievedContext(message, context.MessageRecieved);

                            if (messageRecievedContext.Message.UserId != context.UserId.ToString(CultureInfo.InvariantCulture))
                            {
                                ThreadPool.QueueUserWorkItem(ProcessMessage, messageRecievedContext);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Robot.Log.WriteError(string.Format("An error occured when listening in room {0}", context.RoomId), ex);
                Thread.Sleep(1000);
                Listen(context);
            }
        }