Java Server Pusher Chat:需要在Java服务器和android上进行Pusher一对一聊天的架构[关闭]
Posted
技术标签:
【中文标题】Java Server Pusher Chat:需要在Java服务器和android上进行Pusher一对一聊天的架构[关闭]【英文标题】:Java Server Pusher Chat: needs Architecture for Pusher one to one chat on java server as well as android [closed] 【发布时间】:2017-01-12 14:27:37 【问题描述】:我需要 Pusher android 架构和 Java 服务器端实现以进行一对一聊天。
以下是运行良好的 android 代码,我也收到了 auth
。
"event":"pusher:subscribe","data":"auth":"9272f25541ab7b04fc60:e146b0b663fdfe276af5e4d45de6db7db272bd103414cc9525b998c5eff35268","channel_data":"\"user_id\":\"12345\",\"user_info\":\"name\":\"Phil Leggetter\",\"twitterId\":\"@leggetter\"","channel":"presence-channel"
这里还有 Android 代码:
final HttpAuthorizer authorizer = new HttpAuthorizer(Constant.BASE_URL_BHARTI + "initial/pusher/auth");
//authorizer.setHeaders(getMapAuthorizationHeaders());
PusherOptions options = new PusherOptions().setAuthorizer(authorizer);
final Pusher pusher = new Pusher("9272f25541ab7b04fc60", options);
pusher.connect(new com.pusher.client.connection.ConnectionEventListener()
@Override
public void onConnectionStateChange(ConnectionStateChange connectionStateChange)
Log.e("connectionStateChange", connectionStateChange.getCurrentState().toString());
if (connectionStateChange.getCurrentState() == ConnectionState.CONNECTED)
SOCKET_ID = pusher.getConnection().getSocketId();
Log.e("SOCKET_ID", "" + SOCKET_ID);
channel = pusher.subscribePresence("presence-channel", new PresenceChannelEventListener()
@Override
public void onUsersInformationReceived(String channelName, Set<User> users)
Log.e("PUSHER", "" + channelName + " " + new Gson().toJsonTree(users));
@Override
public void userSubscribed(String channelName, User user)
Log.e("PUSHER", "Channel userSubscribed authorization succeeded " + channelName);
@Override
public void userUnsubscribed(String channelName, User user)
Log.e("PUSHER", "Channel userUnsubscribed authorization err " + channelName);
@Override
public void onAuthenticationFailure(String message, Exception e)
Log.e("PUSHER", "Channel subscription authorization failed " + e);
@Override
public void onSubscriptionSucceeded(String channelName)
Log.e("PUSHER", "Channel subscription authorization succeeded " + channelName);
CHANELNAME = channelName;
@Override
public void onEvent(String channelName, String eventName, String data)
Log.e("PUSHER", "An event with name " + eventName + " was delivered!!" + " " + data + " " + channelName);
Gson gson = new Gson();
Message message = gson.fromJson(data, Message.class);
try
messageAdapter.add(message);
messagesView.setSelection(messageAdapter.getCount() - 1);
catch (Exception e)
Log.e("Exception", "" + e);
,"my-event");
现在我需要 Java 服务器端代码实现来进行一对一聊天。
【问题讨论】:
【参考方案1】:你可以参考这里Pusher one to one chat structure
Java 服务器端实现的第一个要求,您必须在 pom.xml
中添加依赖项。
<dependency>
<groupId>com.pusher</groupId>
<artifactId>pusher-http-java</artifactId>
<version>1.0.0</version>
</dependency>
与
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
否则你会得到500 error
如果您将 JAX-RS Web 服务与 Presence Channel 一起使用,那么您的 /pusher/auth
服务可以写成:
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
@Path("/pusher/auth")
public String processMessagesAndAuthentication( MultivaluedMap<String, String> input)
Pusher pusher = new Pusher("APP-ID", "PUSHER-KEY", "PUSHER-SECRET-KEY");
String userId = "12345"; //any unique id
Map<String, String> userInfo = new HashMap<>();
userInfo.put("name", "Bharti Rawat");
userInfo.put("twitterId", "@leggetter");
String socket_id = input.getFirst("socket_id");
String channel_name = input.getFirst("channel_name");
String auth = pusher.authenticate(socket_id, channel_name, new PresenceUser(userId, userInfo));
return auth;
【讨论】:
请详细说明以上是关于Java Server Pusher Chat:需要在Java服务器和android上进行Pusher一对一聊天的架构[关闭]的主要内容,如果未能解决你的问题,请参考以下文章