WebSockets vs raw TCP sockets in Flash

前端 未结 1 1093
眼角桃花
眼角桃花 2020-12-24 08:42

What WebSockets add to raw TCP connection? Why should I use WebSockets?

I\'d like to hear cons and pros like:

  • Good: WebSockets add some useful things l
相关标签:
1条回答
  • 2020-12-24 09:12

    You can't do raw sockets from a web application in a browser. Even "raw" socket connections from Flash are not really raw because you have to answer a policy file request to get CORS security (part of the reason for the WebSockets handshake).

    After the initial WebSocket handshake, WebSocket messages have two bytes of framing overhead per frame (Hixie-* has '\x00...\xff' and HyBi-07 has two byte header), so the overhead is pretty negligible compared to regular sockets.

    The WebSocket handshake is an HTTP compatible Upgrade request so it is easy to integrate WebSockets support into existing web servers and to use existing Web ports (80/443) which means that WebSocket connection can also more easily integrate into existing firewall rules.

    The HTTP compatible handshake also means that existing HTTP authentication mechanisms can work transparently with WebSockets. Also, WebSockets can be proxied by existing web proxies with little or no modification.

    In the next revision of the WebSockets protocol rev (HyBi-07), their is protection against misbehaving web intermediaries using client to server XOR masking of the payload data.

    Things like auto-reconnection, session ids, etc aren't defined in WebSockets although several Javascript frameworks built on WebSockets have this such as Socket.IO. If you are doing WebSockets from Flash applications then you would need to do your own session management or convert an existing session management library to use WebSockets rather than Flash sockets (such be pretty easy conversion).

    Update:

    Couple of links that might be useful to you or others who land here:

    • AS3WebSocket: WebSockets client library for Flash applications.
    • web-socket-js: WebSockets fallback/polyfill implemented in Flash for Javascript applications (to add WebSockets support to browsers with Flash but without native WebSockets).
    0 讨论(0)
提交回复
热议问题