Exemple #1
0
        public async Task StreamingLocalTimeline()
        {
            ClientWebSocket ws = new ClientWebSocket();

            try
            {
                await ws.ConnectAsync(new Uri(string.Format("ws://{0}/api/v1/streaming/?access_token={1}&stream=public:local", host, token)), CancellationToken.None);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("{0} への接続に失敗しました。", host);
                Console.Error.WriteLine(e);
                return;
            }

            var utf = new UTF8Encoding();

            streaming = true;
            while (ws.State == WebSocketState.Open)
            {
                var buffer  = new byte[BUFFER_SIZE];
                var segment = new ArraySegment <byte>(buffer);
                WebSocketReceiveResult recieved = await ws.ReceiveAsync(segment, CancellationToken.None);

                if (recieved.EndOfMessage)
                {
                    string str = utf.GetString(buffer, 0, recieved.Count);
                    try
                    {
                        JObject obj1 = JObject.Parse(str);
                        switch ((string)obj1["event"])
                        {
                        case ("update"):
                            JObject     payload = JObject.Parse((string)obj1["payload"]);
                            Item.Status status  = JsonConvert.DeserializeObject <Item.Status>((string)obj1["payload"]);
                            updater.Update(status);
                            break;

                        case ("delete"):
                            string id = (string)obj1["payload"];
                            updater.Delete(id);
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine("Error: at parsing {0}", str);
                        Console.Error.WriteLine(e);
                    }
                }
                else
                {
                    Console.Error.WriteLine("recieved message is too long.");
                }
            }

            streaming = false;
            Console.WriteLine("Streaming end");
        }
Exemple #2
0
        public void AfterDelete(IEntity entity)
        {
            IUpdater obj = CacheUpdater.Get(entity);

            if (obj != null)
            {
                obj.Delete(entity);
            }
        }