socket、socketio、flask-socketio、WebSocket的區別與聯繫php
總結html
HTTP、WebSocket 等應用層協議,都是基於 TCP 協議來傳輸數據的。咱們能夠把這些高級協議理解成對 TCP 的封裝。
既然你們都使用 TCP 協議,那麼你們的鏈接和斷開,都要遵循 TCP 協議中的三次握手和四次揮手,只是在鏈接以後發送的內容不一樣,或者是斷開的時間不一樣。
對於 WebSocket 來講,它必須依賴 HTTP 協議進行一次握手 ,握手成功後,數據就直接從 TCP 通道傳輸,與 HTTP 無關了。html5
在源碼flask_socketio.SocketIO#run方法中能夠看出 select 多路複用的幾種選擇。python
if self.server.eio.async_mode == 'threading': from werkzeug._internal import _log _log('warning', 'WebSocket transport not available. Install ' 'eventlet or gevent and gevent-websocket for ' 'improved performance.') app.run(host=host, port=port, threaded=True, use_reloader=use_reloader, **kwargs) # 這裏的 app 就是 app = Flask(__name__) elif self.server.eio.async_mode == 'eventlet': def run_server(): import eventlet import eventlet.wsgi import eventlet.green addresses = eventlet.green.socket.getaddrinfo(host, port) if not addresses: raise RuntimeError('Could not resolve host to a valid address') eventlet_socket = eventlet.listen(addresses[0][4], addresses[0][0]) # If provided an SSL argument, use an SSL socket ssl_args = ['keyfile', 'certfile', 'server_side', 'cert_reqs', 'ssl_version', 'ca_certs', 'do_handshake_on_connect', 'suppress_ragged_eofs', 'ciphers'] ssl_params = {k: kwargs[k] for k in kwargs if k in ssl_args} if len(ssl_params) > 0: for k in ssl_params: kwargs.pop(k) ssl_params['server_side'] = True # Listening requires true eventlet_socket = eventlet.wrap_ssl(eventlet_socket, **ssl_params) eventlet.wsgi.server(eventlet_socket, app, log_output=log_output, **kwargs) if use_reloader: run_with_reloader(run_server, extra_files=extra_files) else: run_server() elif self.server.eio.async_mode == 'gevent': from gevent import pywsgi try: from geventwebsocket.handler import WebSocketHandler websocket = True except ImportError: websocket = False
http://www.javashuo.com/article/p-kfxcfwrn-h.htmlweb