WebSockets vs raw TCP sockets in Flash

2019-01-31 22:39发布

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 like autoreconnection, session ids, etc.
  • Bad: WebSockets add a lot of overhead

I'll have only Flash clients, no need to support Javascript clients.

1条回答
等我变得足够好
2楼-- · 2019-01-31 23:29

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).
查看更多
登录 后发表回答