|
20金钱
最近想做一个Egret(HTML5)WebSocket通讯测试 , 因为物品对于C#比较熟悉 , 所以选择了使用C#作为WebSocket服务端。现在的情况是:
H5发送的消息 C#服务端能够完美的接收并显示 , 但是C#发送的消息到H5客户端,H5得到的消息长度始终为0。
我将Egret的WebSocket连接到其官网 , 测试客户端是正确的。
服务端:
#region 向服务端发送信息
public void Send2Msg(Socket client, string clientMsg)
{
Console.WriteLine("发送数据:“" + clientMsg + "” 至客户端....");
byte[] body = Encoding.UTF8.GetBytes(clientMsg);
byte[] c = this.PackData(body);
Console.WriteLine("数据的大小 : {0} / 包的大小 : {1}", body.Length , c.Length);
client.Send(c);
}
/// <summary>
/// 打包服务器数据
/// </summary>
/// <param name="message">需要打包的数据(通讯用)</param>
/// <returns>数据包</returns>
private byte[] PackData(byte[] message)
{
byte[] contentBytes = null;
if (message.Length < 126)
{
contentBytes = new byte[message.Length + 2];
contentBytes[0] = 0x81;
contentBytes[1] = (byte)message.Length;
Array.Copy(message, 0, contentBytes, 2, message.Length);
}
else if (message.Length < 0xFFFF)
{
contentBytes = new byte[message.Length + 4];
contentBytes[0] = 0x81;
contentBytes[1] = 126;
contentBytes[2] = (byte)(message.Length >> 8 & 0xFF);
contentBytes[3] = (byte)(message.Length & 0xFF);
Array.Copy(message, 0, contentBytes, 4, message.Length);
}
else
{
contentBytes = new byte[message.Length + 10];
contentBytes[0] = 0x81;
contentBytes[1] = 127;
byte[] ulonglen = BitConverter.GetBytes((long)message.Length);
contentBytes[2] = ulonglen[7];
contentBytes[3] = ulonglen[6];
contentBytes[4] = ulonglen[5];
contentBytes[5] = ulonglen[4];
contentBytes[6] = ulonglen[3];
contentBytes[7] = ulonglen[2];
contentBytes[8] = ulonglen[1];
contentBytes[9] = ulonglen[0];
Array.Copy(message, 0, contentBytes, 10, message.Length);
}
return contentBytes;
}
有什么不对吗 ? 请各位大神指教
|
|