2

Im writing custom Websocket server using c#. I wrote some code, according to this mozilla guide. Now I'm succesfully handshaking and receiving messages from my client, and all it working on Chrome, but when I'm trying to send message from server on FireFox, im getting console error "The connection to ws://localhost:80 was interrupted while the page was loading". I'm sending Encoded message, using alghorithm from this page and client side exactly the same from websocket echo test. You can find whole project on GitHub

I've tried sending not encoding bytes and reopen websoket connection each time i send a message).

As you see, server automatically sends message "Hello" to the client. Process Function(new thread for each client):

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 Function(EncodeMessage - algorithm from link above, nicely working with chrome)

private void SendMessageFromServer(string message)
        {
            Byte[] messageByte = _webSocketConverter.EncodeMessage(message);            
            Stream.Write(messageByte);
        }

Seems like problem with server, because websocket.org/echo working with firefox.

  • If I remember correctly, Firefox was using a different case-lettering in the `Upgrade` header (something like `WebSocket` instead of `websocket`) You will have to make sure you're testing for any letter-case combination. – Myst Jun 12 '19 at 08:03
  • Have tried, not working. Got headers from official mozilla website, it should be correct. Also tried adding protocol "chat", not working either – Eduard Semenkin Jun 13 '19 at 20:12
  • I posted an answer with the Firefox headers and noted the odd `Connection` header. I suspect this is the cause. I apologize that my memory mislead me regarding the identity of the weird header. – Myst Jun 13 '19 at 20:28

2 Answers2

1

A possible solution could be related to the Firefox Connection header.

Comparing WebSocket connection requests on Chrome and Firefox exposes the fact that the Connection header on Firefox is "keep-alive, Upgrade" instead of "Upgrade" (on Chrome).

A possible solution would be to test for the existence of Upgrade (case-insensitive) in the header rather then testing for total header equality.

The full WebSocket request on Chrome (on my machine) looked like:

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

While The full WebSocket request on Firefox read:

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
Myst
  • 18,516
  • 2
  • 45
  • 67
  • Thank you very much for answering, very appreciate by your attention. – Eduard Semenkin Jun 14 '19 at 21:09
  • @EduardSemenkin - sure, I'm happy to help. If an answer helped solve your issue, please accept it (mark it as accepted). You can always "accept" answers to questions you ask. Feel free to upvote answers as well, if they help :-) ... (though I understand my answer probably didn't help much) – Myst Jun 14 '19 at 21:13
  • anyway, thank you for helping. How can I give u likes or something? New to stackoverflow – Eduard Semenkin Jun 15 '19 at 10:23
  • @EduardSemenkin , it’s okay, I was happy to help anyway, no need for anything else. Generally speaking, in SO we don’t offer “likes” to people, just answers. This way, helpful answers propagate upwards, benefiting the whole developer community and unhelpful answers fade away. My answer wasn’t helpful for you. If someone else finds it helpful, they might upvote it. If not, it just proves that it should fade away. – Myst Jun 15 '19 at 10:34
0

Thank you all for answering. Found issue! It was my Encoding function(wesocketconverter.cs). I initialized formatted array as raw array.length + 10. So, I got 8 extra empty bytes. Chrome deal with it, when extra bytes are even, but firefox not. Soon or later It will end working at chrome.