我已經瘋了。所以我決定尋求幫助。從TCP套接字接收和發送連續數據
場景:在網頁中,通過TCP套接字連接到服務器,在循環中,我逐字節地獲取數據。這些數據是連續的。
問:我如何製作一個接收和發送數據給播放器的WebSocket(html5標籤)。
Default.aspx的
<asp:Literal ID="ltrPlayer" runat="server" />
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string tele = HttpContext.Current.Request.QueryString["tele"];
string ramal = HttpContext.Current.Request.QueryString["ramal"];
string dac = HttpContext.Current.Request.QueryString["dac"];
string cti = HttpContext.Current.Request.QueryString["cti"];
if ((tele != null) && (ramal != null) && (dac != null) && (cti != null))
{
string strAudio = "<audio controls='controls'>";
strAudio += "<source src='Play.ashx?tele=" + tele + "&ramal=" + ramal + "&dac=" + dac + "&cti=" + cti + "' type='audio/x-wav' controls preload='auto'>";
strAudio += "Seu browser não oferece suporte a este player.";
strAudio += "</audio>";
ltrPlayer.Text = strAudio;
}
}
Play.ashx
<%@ WebHandler Language="C#" Class="Play" %>
using System;
using System.Web;
using System.Net;
using System.IO;
using Alvas.Audio; // Make the conversion VOX to WAV
using System.Media;
using System.Configuration;
using System.Net.Sockets;
using System.Text;
public class Play : IHttpHandler
{
private NetworkStream ns;
public void ProcessRequest(HttpContext context)
{
string tele = HttpContext.Current.Request.QueryString["tele"];
string ramal = HttpContext.Current.Request.QueryString["ramal"];
string dac = HttpContext.Current.Request.QueryString["dac"];
string cti = HttpContext.Current.Request.QueryString["cti"];
if ((tele != null) && (ramal != null) && (dac != null) && (cti != null))
{
try
{
TcpClient oClient = new TcpClient();
oClient.Connect(tele, 22000);
ns = oClient.GetStream();
write(ns, "ondelogar");
ns = oClient.GetStream();
write(ns, "monitorarRamalViaRede(tele;ramal;dac;cti)");
Thread.Sleep(1000);
do
{
if (oClient.GetStream().DataAvailable == true)
{
using (StringReader reader = new StringReader(read(ns)))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Byte[] byteOut = new Byte[line.Length/2];
int i = 0;
while (i < (line.Length/2))
{
byteOut[i/2] = Convert.ToByte(line.Substring(i, 2), 16);
i = i + 2;
}
Stream s = new MemoryStream(byteOut);
BinaryReader br = new BinaryReader(s);
MemoryStream pcmStream = new MemoryStream();
IntPtr pcmFormat = AudioCompressionManager.GetPcmFormat(1, 16, 6000);
WaveWriter ww = new WaveWriter(pcmStream, AudioCompressionManager.FormatBytes(pcmFormat));
Vox.Vox2Wav(br, ww);
WaveReader wr = new WaveReader(pcmStream);
byte[] pcmData = pcmStream.ToArray();
br.Close();
ww.Close();
wr.Close();
pcmStream.Close();
Array.Clear(byteOut, 0, byteOut.Length);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename=\"{0}\"", "BufferDeAudio"));
HttpContext.Current.Response.ContentType = "audio/x-wav";
HttpContext.Current.Response.BinaryWrite(pcmData);
HttpContext.Current.Response.End();
}
}
}
}
while (oClient.GetStream().DataAvailable == true);
ns.Close();
}
catch (Exception)
{
}
HttpContext.Current.Response.End();
}
}
private void write(NetworkStream ns, string message)
{
byte[] msg = Encoding.ASCII.GetBytes(message + Environment.NewLine);
ns.Write(msg, 0, msg.Length);
}
private string read(NetworkStream ns)
{
StringBuilder sb = new StringBuilder();
if (ns.CanRead)
{
byte[] readBuffer = new byte[1024];
int numBytesRead = 0;
do
{
numBytesRead = ns.Read(readBuffer, 0, readBuffer.Length);
sb.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer, 0, numBytesRead));
sb.Replace(Convert.ToChar(24), ' ');
sb.Replace(Convert.ToChar(255), ' ');
sb.Replace('?', ' ');
}
while (ns.DataAvailable);
}
return sb.ToString();
}
public bool IsReusable
{
get
{
return false;
}
}
}
然後,作爲循環由線接收數據線,我發送給玩家。但我聽到的唯一聲音是循環的最後一行。
我試圖把玩家放在一個沒有成功的線程中。
這可能嗎?任何想法?
檢查了嗎? http://codingsample.blogspot.ch/2008/06/streaming-sound-in-c.html –