erizet / serversentevent4net Goto Github PK
View Code? Open in Web Editor NEWServer-Sent Event(SSE) implementation for ASP.NET WebApi.
Server-Sent Event(SSE) implementation for ASP.NET WebApi.
When first event is sent client don't recieve it. Then events are recieved with delay. That is first sent event are recieved on second time, second event on third time.
Sometimes client may recieve two events and on the next time skip again.
I know that it is promblem in PushStreamContent and I also couldn't solve this problem before using ServerSentEvent4Net
表现:启用心跳,一段时间后,断开客户端。然后程序会在不存在的连接上试图发送心跳,因为缺少了异常的捕获,导致服务端崩溃,
异常如下:
System.IO.IOException ---> System.Net.HttpListenerException: 企图在不存在的网络连接上进行操作。
在 System.Net.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 size)
在 Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.Write(Byte[] buffer, Int32 offset, Int32 count)
--- 内部异常堆栈跟踪的结尾 ---
在 Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.Write(Byte[] buffer, Int32 offset, Int32 count)
在 System.Net.Http.Internal.DelegatingStream.Write(Byte[] buffer, Int32 offset, Int32 count)
在 System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
在 System.IO.StreamWriter.Flush()
在 ServerSentEvent4Net.ServerSentEvent.Client.Send(IMessage msg) 位置 D:\Code\TFS2012\QinQouShui\QouShui\QProtocol\ServerSentEvent4Net\ServerSentEvent.cs:行号 240
在 ServerSentEvent4Net.ServerSentEvent.<>c__DisplayClass24_0.b__0(Client c) 位置 D:\Code\TFS2012\QinQouShui\QouShui\QProtocol\ServerSentEvent4Net\ServerSentEvent.cs:行号 120
在 System.Collections.Generic.List1.ForEach(Action
1 action)
在 ServerSentEvent4Net.ServerSentEvent.SendAndRemoveDisconneced(List`1 clientsToSendTo, Message msg) 位置 D:\Code\TFS2012\QinQouShui\QouShui\QProtocol\ServerSentEvent4Net\ServerSentEvent.cs:行号 120
在 ServerSentEvent4Net.ServerSentEvent.Send(Message msg) 位置 D:\Code\TFS2012\QinQouShui\QouShui\QProtocol\ServerSentEvent4Net\ServerSentEvent.cs:行号 108
修正方法:增加通用异常捕获,修正后的代码如下:
` public void Send(IMessage msg)
{
try
{
// Only send retry once for each connection
if (IsRetrySent)
msg.Retry = null;
var text = msg.ToString();
StreamWriter.WriteLine(text);
StreamWriter.Flush();
if (!Message.IsOnlyComment(msg))
LastMessageId = msg.Id;
if (!string.IsNullOrWhiteSpace(msg.Retry))
IsRetrySent = true;
}
catch (HttpException ex)
{
if (ex.ErrorCode == -2147023667) // The remote host closed the connection.
{
IsConnected = false;
}
}
catch (System.ServiceModel.CommunicationException)
{
IsConnected = false;
}
**catch (IOException ex)
{
//IO异常,可判定为已断开
IsConnected = false;
Console.WriteLine("SSE Send Error:" + GetErr(ex));
}
catch (Exception ex)
{
Console.WriteLine("SSE Send Error:" + GetErr(ex));
}**
}
string GetErr(Exception ex)
{
Exception innerException = ex.InnerException;
string err = ex.Message;
while (innerException != null)
{
err = innerException.Message;
innerException = innerException.InnerException;
}
return err;
}`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.