2014-05-12 2 views
1

닷넷의 XSocket을 처음 접했습니다.XSockets.Net으로 대용량 파일 보내기

내 서버 코드는 다음과 같습니다. 다음

public class MyChatController : XSocketController 
{ 

    public void Foo(ITextArgs textArgs) 
    { 
     this.SendToAll(textArgs); 
    } 

} 

는 클라이언트 측 코드

static void Main(string[] args) 
    { 
     var client = new XSocketClient("ws://127.0.0.1:4502/MyChat","*"); 

     client.OnOpen += (sender, eventArgs) => Console.WriteLine("OPEN"); 
     client.Bind("foo", message=> 
     { 
      dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(message.data); 

      var array = data; 

      byte[] bytes = Convert.FromBase64String(array); 

      Stream readStream = new MemoryStream(bytes);//videovm.video 
      var fileName = "C:\\Users\\NandaKishore\\Documents\\Visual Studio 2013\\Projects\\CSClientApp\\CSClientApp\\somefile" + ".mp4"; 

      string targetPath = fileName; 

      FileStream writeStream = new FileStream(targetPath, FileMode.Create, FileAccess.Write); 

      int Length = 256; 
      Byte[] buffer = new Byte[Length]; 
      int bytesRead = readStream.Read(buffer, 0, Length); 
      while (bytesRead > 0) 
      { 
       writeStream.Write(buffer, 0, bytesRead); 
       bytesRead = readStream.Read(buffer, 0, Length); 
      } 
      readStream.Close(); 
      writeStream.Close(); 

      Console.WriteLine("done dona done"); 
     }); 
     client.Open(); 





     ConsoleKeyInfo cki;    
     Console.WriteLine("Press the Escape (Esc) key to quit and any other key to send a message: \n"); 

     var _FileName = "C:\\Users\\NandaKishore\\Documents\\Visual Studio 2013\\Projects\\CSClientApp\\CSClientApp\\tt.mp4"; 

     byte[] _Buffer = null; 
     try 
     { 
      System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); 
      System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream); 
      long _TotalBytes = new System.IO.FileInfo(_FileName).Length; 
      _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes); 
      _FileStream.Close(); 
      _FileStream.Dispose(); 
      _BinaryReader.Close(); 
     } 
     catch (Exception _Exception) 
     { 
      Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); 
     } 

     do 
     { 
      cki = Console.ReadKey(); 
      if (cki.Key != ConsoleKey.Escape) { 

       var dd = Convert.ToBase64String(_Buffer); 
       client.Send(dd, "foo"); 
      } 
     } while (cki.Key != ConsoleKey.Escape); 
    } 

그것은 작은 이미지와 동영상에 대한 잘 작동하지만 큰 비디오와 함께 실패합니다. 바이트 서비스 또는 기타와 같은 대용량 파일을 전달하는 더 좋은 방법이 있습니까?

모든 안내가 도움이 될 것입니다.

답변

0

WebSockets는 대용량 파일을 스트리밍하는 데 적합하지 않지만 작은 청크로 파일을 보내면 성취 할 수 있습니다. Base64의 평균 크기는 실제 파일보다 ~ 36 % 커야하므로 Base64 문자열을 사용하면 안됩니다.

파일을 슬라이스하여 바이트 배열로 보내고 XSockets의 이진 메서드를 사용합니다. 귀하의 예제 코드에서

당신은 바이너리 메시지 here

ITextArgs 및 IBinaryArgs 모두를 갖는에 대한 자세한 내용을보실 수 있습니다 ... 대신 IBinaryArgs의 ITextArgs를 사용하고, 당신은 Base64로를 사용하고있는 이유도 것은 조금 이상하다 ,하지만 WebSocket은 바이너리 메시지에 대한 지원이 부족하기 때문에 현재 XSocket에서이 방법이 사용되고 있습니다. 버전 4.0 (아직 릴리스되지 않음)에서 IMessage는 ITextArgs 및 IBinaryArgs를 모두 대체하여 모든 유형의 메시지 처리에 대한 지원이 훨씬 향상되었습니다.