python - 端口 443 上的 websocket 与 kubernetes 下的 /socket.io 不工作
问题描述
试图移动我的开发环境以在 minikube 上运行。
页面加载,但我的页面在加载 index.html 的同一端口/协议上使用 websockets(在本例中为 https),并且 websockets 似乎没有正常工作。
这是在我的本地开发框中通过 nginx/python 运行时正确输出的示例。
127.0.0.1 - - [14/Sep/2018 14:14:35] "GET / HTTP/1.0" 200 -
127.0.0.1 - - [14/Sep/2018 14:14:35] "GET /static/jquery.min.js HTTP/1.0" 200 -
127.0.0.1 - - [14/Sep/2018 14:14:35] "GET /static/socket.io.min.js HTTP/1.0" 200 -
127.0.0.1 - - [14/Sep/2018 14:14:35] "GET /socket.io/?EIO=3&transport=polling&t=MNPIg-N HTTP/1.0" 200 -
127.0.0.1 - - [14/Sep/2018 14:14:35] "GET /favicon.ico HTTP/1.0" 404 -
127.0.0.1 - - [14/Sep/2018 14:14:35] "GET /favicon.ico HTTP/1.0" 404 -
127.0.0.1 - - [14/Sep/2018 14:14:35] "POST /socket.io/?EIO=3&transport=polling&t=MNPIg-o&sid=0570b4fe27f345e9b11858b3acb40a6e HTTP/1.0" 200 -
127.0.0.1 - - [14/Sep/2018 14:14:35] "GET /socket.io/?EIO=3&transport=polling&t=MNPIg-r&sid=0570b4fe27f345e9b11858b3acb40a6e HTTP/1.0" 200 -
127.0.0.1 - - [14/Sep/2018 14:14:35] "POST /socket.io/?EIO=3&transport=polling&t=MNPIg_x&sid=0570b4fe27f345e9b11858b3acb40a6e HTTP/1.0" 200 -
127.0.0.1 - - [14/Sep/2018 14:14:35] "GET /socket.io/?EIO=3&transport=polling&t=MNPIg_w&sid=0570b4fe27f345e9b11858b3acb40a6e HTTP/1.0" 200 -
127.0.0.1 - - [14/Sep/2018 14:14:40] "GET /socket.io/?EIO=3&transport=polling&t=MNPIh0L&sid=0570b4fe27f345e9b11858b3acb40a6e HTTP/1.0" 200 -
127.0.0.1 - - [14/Sep/2018 14:14:45] "GET /socket.io/?EIO=3&transport=polling&t=MNPIiE3&sid=0570b4fe27f345e9b11858b3acb40a6e HTTP/1.0" 200 -
127.0.0.1 - - [14/Sep/2018 14:14:50] "GET /socket.io/?EIO=3&transport=polling&t=MNPIjSI&sid=0570b4fe27f345e9b11858b3acb40a6e HTTP/1.0" 200 -
127.0.0.1 - - [14/Sep/2018 14:14:55] "GET /socket.io/?EIO=3&transport=polling&t=MNPIkgS&sid=0570b4fe27f345e9b11858b3acb40a6e HTTP/1.0" 200 -
注意每 5 秒有一个 GET (这是一个在页面上运行的计时器)
在 Kubernetes 上运行时,页面加载并且计时器显示为好像 websocket 已经工作了,但是我没有显示 websocket 在初始日志之后有 GET 或 POST 的日志。
192.168.99.1,172.17.0.7 - - [14/Sep/2018 18:24:03] "GET /static/jquery.min.js HTTP/1.1" 304 1210 0.008244
192.168.99.1,172.17.0.7 - - [14/Sep/2018 18:24:03] "GET /static/socket.io.min.js HTTP/1.1" 304 1210 0.009271
(10) accepted ('172.17.0.7', 34444)
192.168.99.1,172.17.0.7 - - [14/Sep/2018 18:24:04] "GET /socket.io/?EIO=3&transport=polling&t=MNPKrsy HTTP/1.1" 200 379 0.003682
(10) accepted ('172.17.0.7', 34446)
192.168.99.1,172.17.0.7 - - [14/Sep/2018 18:24:04] "GET /favicon.ico HTTP/1.1" 404 1314 0.004694
(10) accepted ('172.17.0.7', 34448)
(10) accepted ('172.17.0.7', 34450)
(10) accepted ('172.17.0.7', 34452)
192.168.99.1,172.17.0.7 - - [14/Sep/2018 18:24:04] "GET /socket.io/?EIO=3&transport=polling&t=MNPKrtD&sid=77d4755c524f47c2948b9c36da007b85 HTTP/1.1" 200 210 0.000749
192.168.99.1,172.17.0.7 - - [14/Sep/2018 18:24:04] "POST /socket.io/?EIO=3&transport=polling&t=MNPKrtB&sid=77d4755c524f47c2948b9c36da007b85 HTTP/1.1" 200 194 0.002632
(10) accepted ('172.17.0.7', 34454)
192.168.99.1,172.17.0.7 - - [14/Sep/2018 18:24:04] "GET /favicon.ico HTTP/1.1" 404 1314 0.002388
Ingress 设置如下:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: websitev2-cluster-ip-service
servicePort: 8080
如前所述,websocket 不驻留在不同的端口上,它在 javascript 中被实例化为:
namespace = '/socket';
var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port + namespace);
让 websockets 工作有什么特殊要求吗?我不相信我需要一个特殊的路由,因为 URI 需要发送到与其他所有内容相同的位置,并且在同一个端口上。
编辑:更多细节
我的网站有一个表单,提交时执行以下代码:
$('form#job').submit(function(event) {
var nameValue = JSON.stringify($(event.target).serializeArray());
console.log(nameValue)
socket.emit('job_submit', {data: nameValue});
return false;
});
在 python 方面,我有我的套接字代码,一旦用户单击提交按钮,它就会被命中。
@socketio.on('job_submit', namespace='/socket')
def job_submit(message):
print('recieved job_submit message from client')
# kick off subjob in celery task
data = unstringify(message)
print('data = {0}'.format(data))
sessiondata = dict(session)
print('sessiondata = {}'.format(sessiondata))
subjobstart.delay(sessiondata, request.sid, data)
在我的日志中,当单击提交按钮时,我没有收到任何“从客户端收到 job_submit 消息”消息,这意味着 javascript 正在尝试将 websocket 发送到 python 服务器,但消息永远不会到达那里。
随着时间在网站上更新,从 python 到客户端页面的发射正在工作。
解决方案
所以不完全确定它在测试盒上工作时发生了什么变化,显然问题在于我正在做的表单验证。一旦我输入了 novalidate 选项来暂时绕过表单检查,表单就可以被提交,毕竟我没有任何 websocket 问题。
希望这篇文章可以帮助某人发布代码。
推荐阅读
- sql - 如何根据我的选择语句中的其他 3 列创建新列
- angularjs - 将服务的值保存在变量中
- mysql - 无法使用联合引擎 mysql 在 ipv6 上连接远程服务器
- apache - 在 > 1024 的端口上运行 apache 容器
- android-recyclerview - 拖放后如何将项目位置存储在 RecyclerView 中
- python - python中一个大列表的总和
- python - 查询关于时区的一天
- sql-server - 将 Microsoft SQL Server 中的文件表链接到 Microsoft Access 2016 中的表
- node.js - Elasticsearch nodejs检查队列是否已满
- html - 具有多个 colspan 的两个表头