我对 .Net 中的 XSockets 非常陌生。
以下是我的服务器代码。
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);
}
它适用于小图像和视频,但对于大视频它会失败。有没有更好的方法来传递大文件,例如字节服务或任何东西。
任何指导都会有所帮助。