Java Server Pusher Chat:需要在Pssher上进行一对一聊天的架构,以及java服务器和android

时间:2017-01-12 14:27:37

标签: java android web-services jax-rs pusher

我需要针对 Pusher android和Java Server Side实现的架构进行一对一聊天。

下面是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 个答案:

答案 0 :(得分: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网络服务以及Presence频道,那么您的/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;
}