Setting up a connection with Websocket

I am trying to learn more about websocket and its internal implementations. But still can not understand a few things. I tried a google search for a detailed explanation, but most of them just provide a high-level overview. Below are my doubts.

1. According to what I read, the web socket server (C # / C ++ implementation) uses port 80 by default. Although we can use any port, it prefers to use port 80 because we are used to having problems with the firewall . If so, how should we start the web server and web socket server on the same port (80)?

2. Assume that the web socket server is running on port 81, and the web server is running on port 80.

  • So, when the browser sends the original request for the HTTP request (Upgrade: websocket), this request is sent to port 81. Is this correct? If so, this request (see below) has nothing to do with the HTTP protocol. But still, we use the headers of the HTTP protocol. Why?

    GET /mychat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat Sec-WebSocket-Version: 13 Origin: http://example.com 
  • Why do they use the same websocket interface, which is currently implemented in most browsers, to directly connect TCP / IP to this port without any HTTP materials?

3. Is there a limit on packet size or a data / buffer limit on data sent / received from the client / server? If so, do we need to create data and process it ourselves?

4. Should the websocket server always be a separate service / process? In the future, web servers (IIS, apache) will include support for hosting web socket servers within their process space?

+7
source share
1 answer
  • Using an HTTP-compatible mapping, you can integrate the WebSocket handler into your web server or simply redirect the web server to a web connection to a dedicated WebSocket server.

  • The WebSocket handshake uses HTTP-compatible mapping, which makes it easy to handle both protocols on the same port and makes existing firewall configurations much easier to support WebSocket traffic. Also, preventing end-to-end script attacks in a context-friendly HTTP request, and therefore WebSocket uses this knowledge. Even after the connection is established, WebSocket is not a socket connection . This is a message-based protocol and therefore requires framing. In addition, the framing is masked when sent from the client (browser) to the server, in order to alleviate concerns about the theoretical vulnerability in the incorrect use of proxies / caches / intermediaries.

  • There are no message size restrictions in the protocol itself. The message can be divided into several frames. There is a frame size limit, but it is 2 ^ 64 bytes. The actual frame size will be smaller depending on the client / server implementation. If you have several megabyte single messages that you want to send, you might consider changing the application to use smaller messages to maximize cross-browser and cross-server support.

  • WebSocket processing can definitely be integrated into web servers, and it was a very script provided by the workgroup. For example, consider pywebsocket , which is designed to run both standalone and mod_python modules in Apache. As another example, ASP.NET 4.5 and IIS 8 will have native support for WebSockets .

+3
source

All Articles