What's the difference between WebSocket and plain socket communication?

后端 未结 2 1617
日久生厌
日久生厌 2020-11-29 23:14

According to the Wikipedia, the only relationship between HTTP and WebSocket is an extra handshake in the form of a Upgrade HTTP request. And after that, it see

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 23:42

    No, WebSockets are more than just plain sockets. They use a framing protocol which requires a handshake and then exchanges messages masked by XORint them with a 32bit random number. For more information, read the RFC which standardizes them.

    The reason for this additional encoding layer is that allowing a web browser to create arbitrary socket connections would open various security problems. You could, for example, make visitors to your website connect to arbitrary mailservers via SMTP and make them send spam without the user realizing. That's why the protocol was designed in a way that any server-sided applications need to implement it intentionally before they can be used from web browsers.

    Regarding ports: By default, WebSocket connects to Port 80, but the API can receive any port. The client-sided port is randomized, like in most TCP/IP-based protocols.

    Why wasn't it implemented earlier? Because until recently the WhatWG and W3C didn't have the unified support by all major browser developers to get the authority they require to introduce new standards. That's why there is such a flood of new browser features under the label HTML5 recently.

提交回复
热议问题