fix web socket message parsing

This commit is contained in:
Luke Pulverenti 2015-03-08 20:36:02 -04:00
parent ecc6df0b71
commit e70e06f0ac
3 changed files with 36 additions and 15 deletions

View File

@ -2,6 +2,7 @@
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Logging;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using WebSocketState = MediaBrowser.Model.Net.WebSocketState;

View File

@ -98,6 +98,9 @@
<Reference Include="ServiceStack.Text">
<HintPath>..\ThirdParty\ServiceStack.Text\ServiceStack.Text.dll</HintPath>
</Reference>
<Reference Include="UniversalDetector">
<HintPath>..\ThirdParty\UniversalDetector\UniversalDetector.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\SharedVersion.cs">

View File

@ -1,4 +1,5 @@
using MediaBrowser.Common.Events;
using System.Text;
using MediaBrowser.Common.Events;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
@ -8,6 +9,7 @@ using System.Collections.Specialized;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using UniversalDetector;
namespace MediaBrowser.Server.Implementations.ServerManager
{
@ -132,28 +134,43 @@ namespace MediaBrowser.Server.Implementations.ServerManager
{
return;
}
var charset = DetectCharset(bytes);
if (string.Equals(charset, "utf-8", StringComparison.OrdinalIgnoreCase))
{
OnReceiveInternal(Encoding.UTF8.GetString(bytes));
}
else
{
OnReceiveInternal(Encoding.ASCII.GetString(bytes));
}
}
private string DetectCharset(byte[] bytes)
{
try
{
//_logger.Debug(Encoding.UTF8.GetString(bytes));
using (var memoryStream = new MemoryStream(bytes))
using (var ms = new MemoryStream(bytes))
{
var info = (WebSocketMessageInfo)_jsonSerializer.DeserializeFromStream(memoryStream, typeof(WebSocketMessageInfo));
var detector = new CharsetDetector();
detector.Feed(ms);
detector.DataEnd();
//info = new WebSocketMessageInfo
//{
// MessageType = stub.MessageType,
// Data = stub.Data == null ? null : stub.Data.ToString()
//};
info.Connection = this;
var charset = detector.Charset;
OnReceive(info);
}
}
catch (Exception ex)
if (!string.IsNullOrWhiteSpace(charset))
{
_logger.ErrorException("Error processing web socket message", ex);
//_logger.Debug("UniversalDetector detected charset {0}", charset);
}
return charset;
}
}
catch (IOException ex)
{
_logger.ErrorException("Error attempting to determine web socket message charset", ex);
}
return null;
}
private void OnReceiveInternal(string message)