【发布时间】:2019-10-26 01:56:56
【问题描述】:
我正在使用 c# 编写自定义 Websocket 服务器。我根据this mozilla 指南编写了一些代码。现在我成功地握手并从我的客户端接收消息,并且所有这些都在 Chrome 上运行,但是当我尝试从 FireFox 上的服务器发送消息时,我收到控制台错误“与 ws://localhost:80 的连接是页面加载时中断”。我正在发送编码消息,使用来自this page 的算法和来自websocket echo test 的客户端完全相同。你可以在GitHub找到整个项目
我尝试发送不编码字节并在每次发送消息时重新打开 websoket 连接)。
如您所见,服务器自动向客户端发送消息“Hello”。 处理函数(每个客户端的新线程):
public void Process()
{
try
{
Stream = _client.GetStream();
HandShake();
while (true)
{
while (_client.Available < 3)
{
}
Byte[] bytes = new Byte[_client.Available];
Stream.Read(bytes, 0, bytes.Length);
var message = GetMessage(bytes);
if (_webSocketConverter.IsClosing(bytes[0]))
{
break;
}
message = GetMessage(bytes);
SendMessageFromServer("Hello");
}
}
catch (Exception ex)
{
throw ex;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.Message);
Console.ResetColor();
}
finally
{
_server.RemoveConnection(this._userInfo.Id);
Console.WriteLine("Client {0} disconnected from the server", this._userInfo.Id);
Close();
}
}
SendMessage 函数(EncodeMessage - 来自上面链接的算法,很好地与 chrome 配合使用)
private void SendMessageFromServer(string message)
{
Byte[] messageByte = _webSocketConverter.EncodeMessage(message);
Stream.Write(messageByte);
}
似乎是服务器的问题,因为 websocket.org/echo 使用 firefox。
【问题讨论】:
-
如果我没记错的话,Firefox 在
Upgrade标头中使用了不同的大小写字母(类似于WebSocket而不是websocket)你必须确保你正在测试任何字母大小写组合。 -
试过了,不行。从官方Mozilla网站获得标题,它应该是正确的。还尝试添加协议“聊天”,也不起作用
-
我用 Firefox 标头发布了一个答案,并注意到奇怪的
Connection标头。我怀疑这是原因。我很抱歉我的记忆误导了我关于奇怪标题的身份。
标签: javascript c# firefox websocket server