我的AngularJS控制器中有以下事件监听器(如PubNub SDK文档中所示),如果在邮件进入后刷新浏览器,则会在一条消息上多次触发。
但data
原始调用Pubnub.getMessageEventNameFor()
之后data
为空,所以除了检查$scope.$on(Pubnub.getMessageEventNameFor($scope.myChannel), function(ngEvent, data) {
$scope.$apply(function() {
processMessage(data.message);
});
});
(这似乎是一个黑客掩盖另一个问题)之外,这个函数是如何/为什么被反复打电话?
using System;
using System.IO;
using System.Collections.Generic;
internal static class StreamReaderExtensions
{
public static IEnumerable<string> ReadUntil(this StreamReader reader, string delimiter)
{
List<char> buffer = new List<char>();
CircularBuffer<char> delim_buffer = new CircularBuffer<char>(delimiter.Length);
while (reader.Peek() >= 0)
{
char c = (char)reader.Read();
delim_buffer.Enqueue(c);
if (delim_buffer.ToString() == delimiter || reader.EndOfStream)
{
if (buffer.Count > 0)
{
if (!reader.EndOfStream)
{
buffer.Add(c);
yield return new String(buffer.ToArray()).Substring(0, buffer.Count - delimeter.Length);
}
else
{
buffer.Add(c);
if (delim_buffer.ToString() != delimiter)
yield return new String(buffer.ToArray());
else
yield return new String(buffer.ToArray()).Substring(0, buffer.Count - delimeter.Length);
}
buffer.Clear();
}
continue;
}
buffer.Add(c);
}
}
private class CircularBuffer<T> : Queue<T>
{
private int _capacity;
public CircularBuffer(int capacity)
: base(capacity)
{
_capacity = capacity;
}
new public void Enqueue(T item)
{
if (base.Count == _capacity)
{
base.Dequeue();
}
base.Enqueue(item);
}
public override string ToString()
{
List<String> items = new List<string>();
foreach (var x in this)
{
items.Add(x.ToString());
};
return String.Join("", items);
}
}
}
有没有办法阻止此侦听器多次触发?
更新:多个火灾各自与先前的消息相关联,而不是相同消息的多次激发。这听起来越来越像Pubnub AngularJS SDK问题。