javascript - Websocket 自定义服务器不适用于 firefox,但适用于 Chrome

标签 javascript c# firefox websocket server

我正在使用 C# 编写自定义 Websocket 服务器。根据this,我写了一些代码莫斯拉指南。现在我成功地握手并从我的客户端接收消息,并且所有它都在 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 Connection header 有关。

比较 Chrome 和 Firefox 上的 WebSocket 连接请求揭示了一个事实,即 Firefox 上的 Connection header 是 "keep-alive, Upgrade" 而不是 "Upgrade"(在 Chrome 上)。

一个可能的解决方案是测试 header 中是否存在 Upgrade(不区分大小写),而不是测试 header 是否完全相等。

Chrome(在我的机器上)上的完整 WebSocket 请求如下所示:

Read: GET /HTTP/1.1
Host: localhost:3000
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://localhost:3000
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Sec-WebSocket-Key: NmFGZCcMdiNlXoW/R+F0lw==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

虽然 Firefox 上的完整 WebSocket 请求读取:

GET / HTTP/1.1
Host: localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:67.0) Gecko/20100101 Firefox/67.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Sec-WebSocket-Version: 13
Origin: http://localhost:3000
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: QU6J0KFZjDA/OgVSATpYDg==
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket

关于javascript - Websocket 自定义服务器不适用于 firefox,但适用于 Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56547178/

相关文章:

javascript - 读取父状态的值并将 bool 值传递给子组件React

javascript - 获取 HTML 中输入字段的值并在链接 url 中使用它

javascript - RequireJS 和 Dojo 冲突

c# - Xamarin IOS 上的推送通知

c# - MySQL.EntityFrameworkCore : Object cannot be cast from DBNull to other types

Javascript 平滑滚动在 Firefox 中不起作用

firefox - 在侧边栏中保留 Firefox 侧边栏链接

javascript - DOM 元素布局更改事件

c#如何根据用户输入更改字体

html - 如何在 Firefox 中专门禁用 HTML5 应用程序缓存