eas*_*ngo 5 python websocket node.js handshake
我有一个 python websocket 服务器和一个 nodejs 客户端,但我无法实现websocket 的协议握手。
以下最小的 websocket 服务器使用Flask-sockets
(使用gevent-websocket)。文件名是ws_server.py:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from flask import Flask, request, Response
from flask_sockets import Sockets
app = Flask(__name__)
sockets = Sockets(app)
@sockets.route('/')
def echo_socket(ws):
# print("type request: ",type(request))
# print("dir request: ", dir(request))
print("request.headers: ", request.headers)
# print("type ws: ",type(ws))
# print("dir ws: ",dir(ws))
if hasattr(request, "Sec-Websocket-Protocol"):
print(request.headers["Sec-Websocket-Protocol"])
else:
print("INFO: No protocol specified")
if request.headers["Sec-Websocket-Protocol"] == "aProtocol":
print("INFO: protocol is OK")
else:
print("INFO: protocol not accepted: closing connection")
ws.close()
while not ws.closed:
message = ws.receive()
if message:
print("received: "+message)
ws.send(message)
if ws.closed:
print("INFO: connection has been closed")
if __name__ == "__main__":
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
server = pywsgi.WSGIServer(('', 5001), app, handler_class=WebSocketHandler)
server.serve_forever()
Run Code Online (Sandbox Code Playgroud)
以下最小的 websocket 客户端使用websocket
库。文件名是app.js:
'use strict'
var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
function connectToServer(uri,protocol){
client.on('connectFailed', function (error) {
console.log('Connect Error: ' + error.toString());
});
client.on('connect', function (connection) {
console.log('WebSocket Client Connected');
connection.on('error', function (error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function () {
console.log('echo-protocol Connection Closed');
});
connection.on('ping', () => {
connection.pong();
});
connection.on('message', function (message) {
if (message.type === 'utf8') {
console.log("Received message is: '" + message.utf8Data + "'");
}
});
console.log("sending SOMETHING");
connection.sendUTF("SOMETHING");
});
client.connect(uri, protocol);
}
const wsHostAndPort = process.env.WSHSTPRT || "ws://echo.websocket.org:80";
const wsProtocol = process.env.WSPRTCL || []; // [] for no protocol
console.log("connecting to: ",wsHostAndPort,"with protocol",wsProtocol);
connectToServer(wsHostAndPort,wsProtocol);
Run Code Online (Sandbox Code Playgroud)
启动python ws服务器:
$ python3 ws_server.py
Run Code Online (Sandbox Code Playgroud)
从 nodejs ws 客户端连接:
$ WSHSTPRT=ws://localhost:5001/ WSPRTCL="aProtocol" node app.js
Run Code Online (Sandbox Code Playgroud)
客户端的输出是
connecting to: ws://localhost:5001/ with protocol aProtocol
Connect Error: Error: Expected a Sec-WebSocket-Protocol header.
Run Code Online (Sandbox Code Playgroud)
服务器终端的输出是:
request.headers: Upgrade: websocket
Connection: Upgrade
Sec-Websocket-Version: 13
Sec-Websocket-Key: +QjH5xejDI+OQZQ0OZcWEQ==
Host: localhost:5001
Sec-Websocket-Protocol: aProtocol
INFO: No protocol specified
INFO: protocol is OK
INFO: connection has been closed
Run Code Online (Sandbox Code Playgroud)
在我看来,python 服务器需要设置一个标题,其名称
"Sec-WebSocket-Protocol"与它从客户端收到的值相同。但我不知道该怎么做。我已经在网上搜索(主要是flask-sockets与gevent-websockets论坛和问题跟踪器)没有任何的运气至今。
我尝试了另一个简单的客户端,websocat. 我是这样调用它的:
$ websocat ws://localhost:5001 --protocol aProtocol
我交互式地提示了一些消息,并且它们被 python 服务器正确响应。它之所以有效,是因为我认为websocat(与 nodejs' 不同websocket)在与服务器的握手中不希望出现“Sec-WebSocket-Protocol 标头”。
但我需要使用需要标头的 nodejs 客户端。所以我的问题是:如何将"Sec-WebSocket-Protocol"
标头合并到 python 服务器握手响应中?
我知道这是一年多前的事了,看起来你已经继续前进了,尽管看起来你已经快到了,只是aProtocol需要替换为subprotocol服务器期望的。
或者,根据Flask-websockets 使用的底层gevent-websocket完全省略:
// Subprotocol (2nd parameter) often is not required at all
var ws = new WebSocket('ws://localhost:5001/api');
Run Code Online (Sandbox Code Playgroud)
对于未来的我或任何在这里绊倒的人,我希望以下内容有所帮助。
在将浏览器连接到 Node.JS apollo-server 订阅的JavaScript 中,我需要以下内容:
// Pasted into Firefox console
const token = '...'; // A bearer token, if auth is needed
const subprotocol = 'graphql-ws';
w = new WebSocket('ws://localhost:5000/graphql', subprotocol);
w.send(JSON.stringify({"type":"connection_init","payload":{"authToken":token}}));
Run Code Online (Sandbox Code Playgroud)
然后我会看到Sec-Websocket-Protocol应用了标题:

否则,如果没有应用子协议,Sec-Websocket-Protocol标头未发送并且控制消息可见,我会看到Connection Closed: 1002并且自然无法将消息发送到 websocket:
| 归档时间: |
|
| 查看次数: |
3277 次 |
| 最近记录: |