gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:49 24 4
gpt4 key购买 nike

我正在客户端订阅“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com