javascript - 如何使用 Pusher 将用户的存款和取款详细信息从客户端发送到 Node.js (express) 服务器?

标签 javascript node.js websocket pusher pusher-js

我正在客户端订阅“my-channel”并绑定(bind)到“my-event”。

pusher.html

<!DOCTYPE html>
<head>
  <title>Pusher Test</title>
  <script src="https://js.pusher.com/5.0/pusher.min.js"></script>
  <script>

    // Enable pusher logging - don't include this in production
    Pusher.logToConsole = true;

    var pusher = new Pusher('******', {
      cluster: 'ap2',
      forceTLS: true
    });

    var channel = pusher.subscribe('my-channel');
    channel.bind('my-event', function(data) {
      alert(JSON.stringify(data));
    });

  </script>
</head>
<body>
  <h1>Pusher Test</h1>
  <p>
    Try publishing an event to channel <code>my-channel</code>
    with event name <code>my-event</code>.
  </p>
</body>

我正在“我的 channel ”中触发“我的事件”,并发送一条消息“hello world”

server.js

        var pusher = new Pusher({
            appId: '****',
            key: '****',
            secret: '******',
            cluster: 'ap2',
            encrypted: true
        });

        pusher.trigger('my-channel', 'my-event', {
            "message": "hello world"
        });

对于此代码,我在客户端收到一条警报消息。即,我的服务器正在向客户端发送消息,但我的问题是我想要整个过程相反,我希望客户端向服务器发送消息以及如何在服务器中接收这些消息?如何使用 Node.js 通过推送器来做到这一点。

谢谢!

最佳答案

Channels 提供的客户端库只能订阅 channel 并消费事件(它们也可以向其他客户端发送事件) - 它们无法向服务器触发事件​​。 服务器库只能触发事件 - 它们不能订阅 channel 。

可以通过使用 client events 来实现解决方案并配置必要的webhooks in your app

更多信息可以在 Pusher Channels documentation 中找到

关于javascript - 如何使用 Pusher 将用户的存款和取款详细信息从客户端发送到 Node.js (express) 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59839323/

相关文章:

javascript - Promise 和不定期的回调

javascript - 如何在纯 JS 中按特定字段(如 _.sortBy)对对象进行排序?

javascript - javascript 中不同大小写的闭包

javascript - Node.js下javascript中的日期函数不准确

javascript - 捕获Webview的请求和响应

android - WebSocket 在几个时间间隔之间不断断开连接

javascript - Safari 扩展中的注入(inject)脚本不会在 Youtube 中触发

javascript - 将焦点放在IE中的输入框

python - 如何从 Django Channels 获取查询参数?

javascript - Nodejs websockets 客户端待处理 promise