我在 ASP.net MVC 网站上将 Pusher Server 配置为:
[HttpPost]
public async Task<ActionResult> HelloWorld() {
var options = new PusherOptions
{
Cluster = "ap2",
Encrypted = true
};
var pusher = new Pusher(
"123",
"ABC",
"XYZ",
options);
var result = await pusher.TriggerAsync(
"my-channel",
"my-event",
new { message = "hello world" } );
return new HttpStatusCodeResult((int)HttpStatusCode.OK);
}
}
我在 Java Swings 中制作了客户端:
PusherOptions options = new PusherOptions();
options.setCluster("ap2");
Pusher pusher = new Pusher("XYZ", options);
Channel channel = pusher.subscribe("my-channel");
channel.bind("my-event", new SubscriptionEventListener() {
@Override
public void onEvent(String channelName, String eventName, final String data)
{
System.out.println(data);
}
});
pusher.connect();
这工作正常,每当我从服务器发送任何消息时,我都会在客户端 JAVA 应用程序中收到消息。
但是我怎样才能将消息连同它的 ClientID 从客户端发送到服务器?
最佳答案
您可以通过不同的方式来做到这一点。如果您有登录用户的 token 或 cookie,您可以(并且应该)将它们用于敏感数据。否则,发送数据基本上可以放入您的 http 正文中,并由您的端点作为参数或复杂对象接收。
public async Task<ActionResult> HelloWorld(string foo, string bar) {
//Stuff goes here
}
通常您的内容类型是 application/json 但您也可以使用其他类型。
然后 body 可以简单地看起来像:
{
"foo":"Hello",
"bar":"World"
}
关于java - 如何使用 Pusher 使用 ClientID 从客户端向服务器发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57265501/