我正在测试使用最新版本的Chrome(websocket版本13)。
这是我位于http://127.0.0.1/folder/default.aspx
的简单客户页面:
<script type="text/javascript">
var socket = new WebSocket('ws://localhost:8181/websession');
socket.onopen = function () {
alert('handshake successfully established. May send data now...');
};
socket.onclose = function () {
alert('connection closed');
};
socket.onmessage = function(msg) {
alert(msg);
};
</script>
这是我的倾听者(c#)
var listener = new TcpListener(IPAddress.Loopback, 8181);
listener.Start();
while (true)
{
Console.WriteLine("Listening...");
using (var client = listener.AcceptTcpClient())
using (var stream = client.GetStream())
using (var reader = new StreamReader(stream))
using (var writer = new StreamWriter(stream))
{
writer.WriteLine("HTTP/1.1 101 Web Socket Protocol Handshake");
writer.WriteLine("Upgrade: WebSocket");
writer.WriteLine("Connection: Upgrade");
writer.WriteLine("WebSocket-Origin: http://127.0.0.1");
writer.WriteLine("WebSocket-Location: ws://localhost:8181/websession");
writer.WriteLine("");
}
Console.WriteLine("Finished");
}
当我运行服务器,然后加载客户端页面时,它只显示"connection closed"
。
有人可以告诉我如何正确执行此握手吗?据我所知,从文档和之前的问题来看,我发送回客户端的响应看起来是正确的。
答案 0 :(得分:3)
根据您的代码,这应该有效(至少使用chrome)
var listener = new TcpListener(IPAddress.Loopback, 8181);
listener.Start();
while (true)
{
Console.WriteLine("Listening...");
using (var client = listener.AcceptTcpClient())
using (var stream = client.GetStream())
using (var reader = new StreamReader(stream))
using (var writer = new StreamWriter(stream))
{
string line = null, key = "", responseKey = "";
string MAGIC_STRING = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
while (line != "")
{
line = reader.ReadLine();
if (line.StartsWith("Sec-WebSocket-Key:"))
{
key = line.Split(':')[1].Trim();
}
}
if (key != "")
{
key += MAGIC_STRING;
using (var sha1 = SHA1.Create())
{
responseKey = Convert.ToBase64String(sha1.ComputeHash(Encoding.ASCII.GetBytes(key)));
}
}
// send handshake to the client
writer.WriteLine("HTTP/1.1 101 Web Socket Protocol Handshake");
writer.WriteLine("Upgrade: WebSocket");
writer.WriteLine("Connection: Upgrade");
writer.WriteLine("WebSocket-Origin: http://127.0.0.1");
writer.WriteLine("WebSocket-Location: ws://localhost:8181/websession");
if (!String.IsNullOrEmpty(responseKey))
writer.WriteLine("Sec-WebSocket-Accept: " + responseKey);
writer.WriteLine("");
writer.Flush();
SendString(stream, "This code works!!!!");
SendString(stream, "This code also works!!!! ".PadRight(300, '.') + "\r\nEND");
}
Console.WriteLine("Finished");
}
void SendString(Stream s, string str)
{
var buf = Encoding.UTF8.GetBytes(str);
int frameSize = 64;
var parts = buf.Select((b, i) => new { b, i })
.GroupBy(x => x.i / (frameSize - 1))
.Select(x => x.Select(y => y.b).ToArray())
.ToList();
for (int i = 0; i < parts.Count; i++ )
{
byte cmd = 0;
if (i == 0) cmd |= 1;
if (i == parts.Count - 1) cmd |= 0x80;
s.WriteByte(cmd);
s.WriteByte((byte)parts[i].Length);
s.Write(parts[i], 0, parts[i].Length);
}
s.Flush();
}
PS:使用
socket.onmessage = function(msg) {
alert(msg.data);
};
查看服务器发送的消息内容。
答案 1 :(得分:2)
您尚未计算安全哈希值。客户端会向您发送一个号码,并且规范中还有另一个硬编码的号码。您需要按照规范中的说明将它们组合起来,并将其作为标题返回。
这表明您的服务器实际上是一个Web套接字服务器。
请参阅RFC6455
请注意,除了RFC之外,您可能还需要考虑是否实现Hixie 76。
答案 2 :(得分:1)