【发布时间】:2020-02-06 13:58:16
【问题描述】:
我使用 pusher_client.authenticate 方法从 pusher 获取存在和私有通道的 socket_id 失败。 这是要理解的架构:
我正在使用烧瓶蓝图。 这是用于识别的服务器网址:
from pusher import Pusher
PUSHER_APP_ID = '123456'
PUSHER_APP_KEY = 'secret109cfb2c29secret'
PUSHER_APP_SECRET = 'PUSHER_APP_SECRET'
PUSHER_APP_CLUSTER = 'eu'
SSL = True
pusher = Pusher(app_id=PUSHER_APP_ID, key=PUSHER_APP_KEY, secret=PUSHER_APP_SECRET, cluster=PUSHER_APP_CLUSTER)
@pousse_bp.route("/pusher/auth", methods=['POST'])
def pusher_authentication():
pusher_client = pusher
auth = pusher_client.authenticate(
channel=request.form['channel_name'],
socket_id=request.form['socket_id'])
flash(auth, 'success')
print(request.json)
return json.dumps(auth)
这是我的模板:
<form method="post" action="/pousse/pusher/auth">
<h3>test authenticate pusher</h3>
<h2>https://pusher.com/docs/channels/server_api/authenticating-users</h2>
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<input type="text" name="channel_name" placeholder="channel_name here"><br>
<input type="submit" name="Submit">
</form>
</body>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://js.pusher.com/4.0/pusher.min.js"></script>
<script type="text/javascript">
var pusher = new Pusher('secret109cfb2c29secret', {cluster: 'eu',encrypted: true, authTransport: 'jsonp',authEndpoint: 'pousse/pusher/auth'});
pusher.connection.bind('connected', function() {
console.log("connected");
socketId = pusher.connection.socket_id;
console.log("socketId" + socketId);
});
var channel = pusher.subscribe('presence-channel');
它不起作用,因为获取 socket_id 有问题, 所以它是无效的。我不明白我的代码中缺少什么。
烧瓶上的消息错误是: 在 pusher_authentication 中
socket_id=request.form['socket_id'])
File "python3.7/site-packages/werkzeug/datastructures.py", line 443, in __getitem__
raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'socket_id'
Mozilla 控制台错误是: 连接普塞指数:88:17 socketId126757.3121110 pousseindex:90:17 Le script à l'adresse « http://localhost:5000/pousse/pousse/pusher/auth?callback=Pus…27%5D&socket_id=126757.3121110&channel_name=presence-channel » a été chargé alors que son type MIME (« text/html ») n'est pas un type MIME JavaScript valide。
【问题讨论】:
-
你能用收到的错误信息编辑问题吗?
标签: authentication flask pusher