0
ASP.net MVC 4 web-api 컨트롤러로 이벤트 종점을 만들려고합니다. 문제는 스트림을 플러시하려고 할 때 예외가 발생한다는 것입니다.서버가 이벤트 연결을 닫았거나 데이터를받지 못했습니다.
The remote host has closed the connection.
또는 클라이언트 측에서 아무 것도받지 못하는 것 같습니다. 스트림에 기록 된 데이터를 플러시 할 수는 있지만.
먼저 ConcurrentQueue 개체를 사용하여 StreamWriter
이라는 식별자를 Guid
이라는 래퍼에 저장했습니다. 이제 ConcurrentDictionary 개체에 키 - 값 쌍으로 저장합니다. 그게 다른 것을 만들어 내는지 나는 모른다.
클라이언트 측 :
<script type="text/javascript">
var event = new EventSource('/back-end/events/tracklisting/866594c8-8411-11e4-9fcb-5254005dbd0a?juke=392fcbe5-8523-e4-9fcb-5254005dbd0a');
event.onopen = function() {
console.log('Onopen event fired!');
};
event.onmessage = function (e) {
console.log('incoming data: ' + e.data);
};
event.onerror = function() {
console.log('Event source error occurred!');
};
</script>
ASP.Net 이벤트 엔드 포인트 :
[HttpGet]
public HttpResponseMessage TrackListing(HttpRequestMessage req, string token, string juke) {
var response = req.CreateResponse();
response.Content = new PushStreamContent((stream, content, ctx) => {
trackListingSubscribers.TryAdd(new Guid(juke), new StreamWriter(stream));
}, "text/event-stream");
response.Headers.Add("Cache-Control", "no-cache");
return response;
}
동시 스트림 저장 :
public static readonly ConcurrentDictionary<Guid, StreamWriter> trackListingSubscribers = new ConcurrentDictionary<Guid, StreamWriter>();
파견 이벤트 기능 :
public static void sendTrackListingEvent(Guid juke)
{
foreach(var subscriber in EventsController.trackListingSubscribers.Where(kvp => kvp.Key == juke)) {
subscriber.Value.WriteLine(string.Format("data: {1}\"refresh\": true,\"juke_id\":\"{0}\" {2} \n", subscriber.Key.ToString(), "{", "}"));
subscriber.Value.Flush();
}
}
,