我想创建一个C#WebSocket服务器,但我似乎并没有得到它的工作。 我现在有一台接受TCPClient的服务器,接收来自客户端的HTTP请求,并尝试发回HTTP响应,以便完成HTML5 WebSocket握手。C#HTML5 Websocket服务器
我相信服务器发送给客户端的握手有问题。 我读了草稿(Websocket 76 draft),其中规定在握手结束时必须给出给出的两个键的响应。该响应由服务器计算。
这是我的代码:
static void Main(string[] args)
{
int port = 8181;
IPAddress localAddr = IPAddress.Loopback;
TcpListener server = new TcpListener(localAddr, port);
server.Start();
// Buffer for reading data
Byte[] receivedBytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Console.WriteLine("Waiting for a connection...");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!\n");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(receivedBytes, 0, receivedBytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.UTF8.GetString(receivedBytes, 0, i);
Console.WriteLine("Received:");
Console.WriteLine(data);
Byte[] response_token = hashResponse(data);
string handshake = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
+ "Upgrade: WebSocket\r\n" + "Connection: Upgrade\r\n"
+ "Sec-WebSocket-Origin: http://localhost\r\n"
+ "Sec-WebSocket-Location: ws://localhost:8181/websession\r\n"
+ "\r\n";
Byte[] writtenBytes = Encoding.UTF8.GetBytes(handshake);
stream.Write(writtenBytes, 0, writtenBytes.Length);
stream.Write(response_token, 0, response_token.Length);
Console.WriteLine("Send:");
Console.WriteLine(handshake);
string strHash = Encoding.UTF8.GetString(response_token);
Console.WriteLine(strHash);
}
}
}
static Byte[] hashResponse(string receivedData)
{
string strDel = "\r\n";
char[] delimeter = strDel.ToCharArray();
string Key1 = null;
string Key2 = null;
string hash = null;
MD5 md5 = MD5.Create();
string[] lines = receivedData.Split(delimeter);
Key1 = lines[10].Substring(20);
Key2 = lines[12].Substring(20);
hash = lines[16];
Int64 numbersKey1 = Convert.ToInt64(string.Join(null, Regex.Split(Key1, "[^\\d]")));
Int64 numbersKey2 = Convert.ToInt64(string.Join(null, Regex.Split(Key2, "[^\\d]")));
Int64 numberSpaces1 = countSpaces(Key1);
Int64 numberSpaces2 = countSpaces(Key2);
int dividedKey1 = (int) (numbersKey1/numberSpaces1);
int dividedKey2 = (int) (numbersKey2/numberSpaces2);
Byte[] encodedKey1 = Encoding.UTF8.GetBytes(dividedKey1.ToString());
Byte[] encodedKey2 = Encoding.UTF8.GetBytes(dividedKey2.ToString());
Byte[] encodedHash = Encoding.UTF8.GetBytes(hash);
Byte[] combined = Encoding.UTF8.GetBytes(dividedKey1.ToString() + dividedKey2.ToString() + hash);
Byte[] responseHash = md5.ComputeHash(combined);
return responseHash;
}
static int countSpaces(string key)
{
int counter = 0;
char[] charArray = key.ToCharArray();
foreach (char c in charArray)
{
if (c.Equals(' '))
counter++;
}
return counter;
}
的HTML页面我使用测试(这是创建一个名为Test.html)由运行在我的电脑上的Apache网络服务器托管我通过浏览访问它(在Chrome)到http://localhost/Test.html
有没有人有线索我做错了,因为我越来越绝望。
在此先感谢
丹尼斯
我不是真的想握手之前接收数据,这是请求客户端发送到我在控制台屏幕上写入的服务器。 – user729932 2011-04-30 11:51:54
在发送握手响应“HTTP/1.1 101 WebSocket协议等”之前,您正在尝试接收数据的一个连接。我认为在您收到连接时您需要发送此消息。在您接收到的数据流之前, – 2011-04-30 18:54:23