简体   繁体   English

如何使用Javascript触发Pusher事件

[英]How to Trigger Pusher event Using Javascript

I just cant trigger a pusher.com event using Javascript 我只是无法使用Javascript触发pusher.com事件

by the way i've Enabled the client events ... 顺便说一句,我已经启用了客户端事件...

i've followed the documentation of the client js script. 我遵循了客户端js脚本的文档。

Any Correct Example ! 任何正确的例子!

First off, in order to send client events, you need an authorization endpoint . 首先,为了发送客户端事件,您需要一个授权端点 This is usually run on the server where your application is hosted. 它通常在托管您的应用程序的服务器上运行。 The javascript library expects the auth endpoint to be located at http://yourapp.com/pusher/auth javascript库期望auth终结点位于http://yourapp.com/pusher/auth

Depending on the kind of server you're running, the way the auth endpoint is implemented will be different. 根据您所运行的服务器的类型,身份验证端点的实现方式将有所不同。 Most major platforms have a Pusher server library already. 大多数主要平台已经具有Pusher 服务器库

Granted, there are ways around having to use an auth endpoint. 当然,有多种方法可以使用身份验证端点。 The only problem is that it requires that your client has access to the Pusher app secret key, which is not the best option security-wise. 唯一的问题是,这要求您的客户端有权访问Pusher应用程序密钥,这不是安全性最好的选择。

You can check out an example of an auth endpoint for use on Google App Engine here . 您可以在此处查看要在Google App Engine上使用的身份验证端点的示例。

Some more details on how the auth endpoint works: 有关auth终结点如何工作的更多详细信息:

It receives POST requests with the following keys: 它使用以下密钥接收POST请求:

socket_id , channel_id socket_id,channel_id

The Pusher javascript library sends a POST request like this Pusher javascript库发送这样的POST请求

example.com:80 POST /pusher/auth?socket_id=123456789&channel_id=private-channel

the response from the auth endpoint is in JSON and looks like this: 来自auth端点的响应是JSON,看起来像这样:

{"auth": "987654321:1234567890abcdef1234567890abcdef"}

where 987654321 is your Pusher Application ID and the rest is the HMAC-SHA256 hash of the Pusher App Secret Key , socket id , and channel name all concatenated together 其中987654321是您的Pusher应用程序ID,其余是Pusher App Secret Keysocket idchannel name都串联在一起的HMAC-SHA256哈希

This auth string is then used by the javascript Pusher library to subscribe to a private channel 然后,该auth字符串将由javascript Pusher库用于订阅私人频道

Everything else in terms of communicating with Pusher is handled by the javascript library over websockets in the browser. 与Pusher通信方面的其他所有内容均由JavaScript库通过浏览器中的websocket处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM